Problems modifying RDLC file at runtime

I am trying to modify a report (rdlc) at runtime.
I have found several examples, but di not succeed (several hours of trying...)
The attached code snippet shows the sentral part of the problem.
The doc.load(stream) gives an error saying something (translated from Norwegian): "Object null reference...."

I seems like I am not able  to locate the rdlc file which is inside the application(?).
I have read something about namespace spesification for the rdlc-file?
But the rdlc file is just a file inside the appilcation?
I am using VS 2005


some code
....
 
ReportViewer1.LocalReport.LoadReportDefinition(JusterGiroDelOppNed("StdUtskriftBrevGiro.rdlc"))
.....
more code...
 
   Private Function JusterGirodelOppNed(ByVal rdlc As String) As IO.TextReader
 
 
        Dim reader As IO.TextReader
        Dim doc As New System.Xml.XmlDocument
 
        Dim stream As IO.Stream
        stream = Me.GetType().Assembly.GetManifestResourceStream(rdlc)
 
        doc.Load(stream)
 
        Dim node As System.Xml.XmlNode
......
more code

Open in new window

JoarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gbzhhuCommented:
So, stream is Nothing (null) right?
0
gbzhhuCommented:
Check the value of rdlc.  Is it valid resource?  note this is case sensitive -- from MSDN  "The case-sensitive name of the manifest resource being requested. "

0
JoarAuthor Commented:
Yes, exactly!
When I debug and point to the "stream" variable, is is "Nothing".
When I point to the "rdlc"-variable it is "StdUtskriftBrevGiro.rdlc", which is the correct name of the report.
The report name is copied and is correct.

The report is running well if I skip the modifying routine.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

gbzhhuCommented:
OK, let's take this step by step.

First we need to be certain that the resource is in the assembly.  so, instead of

stream = Me.GetType().Assembly.GetManifestResourceStream(rdlc)

try

Dim resources() As  String = Me.GetType().Assembly.GetManifestResourceStream(rdlc)

This may have syntax error, I am a Cshapper.  Basically this will return string array of resources.  check what is in the array.  Do you find our item?



0
gbzhhuCommented:
If the item is there then it maybe that it is a private resource.  This is something I heard, not somehting I am knowledgeable.  They say that if a resource is private then your code must have Reflection permission
0
JoarAuthor Commented:
Dim resources() As  String = Me.GetType().Assembly.GetManifestResourceStream(rdlc)
gives me a syntax error

I am looking for the vb equivalent..
If you have the c# syntax I may use a converter..
0
gbzhhuCommented:

c#
string[] resources = this.GetType().Assembly.GetManifestResourceNames();
0
gbzhhuCommented:
Ok, I tried to reproduce this  (in C#)

I added a .rdlc (Report1.rdlc) file to my project.  I tried your code and I get null stream.  I looked at the string array of resources returned by that line I gave you and it shows that my resource is "ContextSensitiveHelp.Report1.rdlc" where ContextSensitiveHelp is my project as well as my namespace name, so try to add your project name with a dot before the reportfile name
0
JoarAuthor Commented:
Converts to:  Dim resources As String() = Me.[GetType]().Assembly.GetManifestResourceNames()

Debugging result: lengt of "resources" = 0
0
gbzhhuCommented:
This is windows forms app right?
0
gbzhhuCommented:
That cannot be right.  I recreated this again in VB project.  Without adding anything to the project
This line
Dim resources As String() = Me.[GetType]().Assembly.GetManifestResourceNames()

gives me 2 items in the string array

"EmbeddedResources.Form1.resources"
"EmbeddedResources.Resources.resources"

EmbeddedResources is my project name.  So there is something else wrong here.  Have you tried adding another resource to the project?

0
JoarAuthor Commented:
No, it is a web form.
Must there be a project name?
When a open my app i VS 2005 I am just pointing at a directory with code.
And what if there are severel namespaces in the app?
Am I doing anything very wrong???
0
JoarAuthor Commented:
Sorry if I am stupid. but I am not sure what a resource is...could you give an example?
0
JoarAuthor Commented:
Sorry I have just one Namespace, but it doesn't matter if I put the namespace name in front of reportname (us.rdlcname)
0
gbzhhuCommented:
A resource is any item that can be embed into the app, like rdlc file, xml file, icon file etc.  Try to add another report file to the project and see what string array contains.  It maybe that the web projects are different.  I will try and create a web project now in VB :-)
0
JoarAuthor Commented:
I highly appreciate your support!
Just now I have to take a break but I will there later...
0
gbzhhuCommented:
No problem.  I created a Web project and the code I gave you does not work in the web project.  It returns no resources :-(  I am trying to find out why.  I am going home too in 45 minutes.
0
gbzhhuCommented:
Hi, I am back, sorry too many meetings today.

OK, I have modified the VB web project and it is now giving me the resources including the report

Try this hardcoded code but replace the assembly path with your site's assembly path and see what you get.  I think the issue is to do with assembly path

        Dim ass As Assembly = Assembly.LoadFrom("C:\Users\hassan.hussain\Documents\Visual Studio 2005\Projects\Reports\bin\Reports.dll")
        Dim resources() As String = ass.GetManifestResourceNames()

Let me know what you get.
0
JoarAuthor Commented:
Hi again,
Had had to put a "System.Reflection." ahead of the .Assemply:
Dim ass As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("c:\webapp\solidus\us\StdUtskriftBrevGiro.rdlc")

but my report is a rdlc-file, not a dll-file (the rdlc is a XML-file which I want to modify...)

I got a runtime error saying: BadImageFormatException was unhandled by user code

Troubleshooting tips: Make sure the fila image is a valid managed assemble
0
gbzhhuCommented:
Hi Joar,

I know your file is rdlc but it is embned into the dll.  Let's check.  If you click on your report file on the solution explorer and you look at the property wondow, there is a property called Build Action, what value does that have?  It should be "Embedded Resource"  When you add the report into the project and set that property to Embedded Resource the report is embed into the dll that your project produces which will be under your BIN folder.  That is why we are trying to load the dll and then we can use the ocde we had earlier that will extract the report from the dll.  Are you with me now?
0
JoarAuthor Commented:
In my case there are only 2 properties for the rdlc file: File Name and Full Path.

I feel I have a problem:
My developing code does not have a bin folder and the reports are not in a dll, until I do publish the site???

Am I misunderstandig?

I am in a meeting all this day, but please do not give me up....
0
gbzhhuCommented:
I have no idea what happened to the other properties :-(

Ok let's find out where that dll is!

        Dim assemblyPath As String = Assembly.GetExecutingAssembly().Location
        Dim ass As Assembly = Assembly.LoadFrom(assemblyPath)
        Dim resources() As String = ass.GetManifestResourceNames()

Use the above code and examine the variable assemblyPath, what do you get?  What about the resources() array?  is there anything in it?

You may not have a Bin folder yet but I think the reports are in the dll unnless there is an other way that I don't know to add reports to projects - which is possible.


How did you add your reports to the project?
0
JoarAuthor Commented:
Sorry for not responding soon. I have been busy yesterday and today. Tomorrow will be better I hope...then I may follow you up more continously if it suits you...

AssemblyPath: C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\us\15fbb6f4\c69110a7\App_Web_gfcjt8rq.dll
Ressources: Length= 0 (means nothing I guess?)

This dll are not in my application, but is some temporary stuff???
There is no dll's at all in my source-code
The bin folder and the dll is created when I publish (compile?) the  app, and the reports is inside

I am adding reports by: Add New Item / report  and chosing the report template
This  create a rdlc-file which is a XML-file
0
gbzhhuCommented:
OK.

I run the same code and I am also getting the temporary file and my resource was empty but I have it fixed though I am not sure which action.  Now try this please.

Right clock on your project->properties click on Web tab. Under the servers option select Use IIS web server.  in the Project URL type http://localhost/<ProjectName> where <ProjectName> is the name of your project or whatever else you want to call.  Click on Create Virtual Directory button.

Build project
Run the same code again and see if you get resources.  

By the way everything you said in your last comment is correct.  I think ASP.NET development server maybe the problem
0
JoarAuthor Commented:
Sorry that you need to be patient with me...

Where do I find the project -> properties??
In some menu in VS 2005 or in some Window pane???
0
gbzhhuCommented:
No worries Joar.

In VS 2005 You have a solution explorer pane.  If it is not there you can show it by clicking on View menu then selectiong Solution Explorer or simply press CTRL + R

On the Solution Explorer your project will be listed in there.  You right click your project then click on Properties.  Any problems let me know.
0
JoarAuthor Commented:
I am using the Solution Explorer window....

The Project property must be what I get if I point to the root directory in the Solution Expl?
This is C:\.... and so on (where my app is)

If I look at the Property there, I have a window popping up with the title: c:\WebApp\Solidus\us  Properti pages

This window has 5 entries in the left pane: References, Build, Accessibility, Start Options, MS Build options
Am I at the right place??
0
gbzhhuCommented:
Are you using VS 2005 Express?

See the 2 files I attached.  That is what my screens look like.  We maybe using different version of VS 2005.


solutionExplorer.jpg
projectProperties.jpg
0
JoarAuthor Commented:
I am using VS 2005 Prof edit.

Look at my 3 attached files

SolExpl-1.bmp
Solexpl-2.bmp
SolExpl3.bmp
0
gbzhhuCommented:
Strange!  I am also using VS 2005 Professional edition but our project property pages look very different.

Can you publish your project to a local folder somewhere on your PC and see if that makes any difference to the result of the code we were testing.
0
JoarAuthor Commented:
First of all I see a difference between these 2 methods og making an application:
1 File / New / Project
0
JoarAuthor Commented:
Sorry for the last comment:
My app is long time ago created this way in VS 2005: File / New / Website

If I create a new app this way: File / New / Project I get your type of property page

I am not shure of the difference between those two methods.
My app has been running well for at least one year.
Any coment on that?
0
gbzhhuCommented:
I don't know what the difference is.

Did you publish project and try again the code?

Since our VS setting seem to be very different I am running out of ideas :-(
0
JoarAuthor Commented:

Yes I did publich to a local catalog (as I do before copying to the webserver)

Bu if I open this published code, then there is no vb-code any more (inside dll's) and I am not allowed to modify anything??

What did you mean I should test?
0
gbzhhuCommented:
Hi,

I am going for lunch.  A colleague and I are looking difference between these web projects.  There is quite a signifcance difference.  I am going to build a new web site the way you did it and trying working out what maybe happening (after lunch)
0
JoarAuthor Commented:
Just to be sure, the main problem is: where do I find the rdlc-file at runtime?
Because when I press F5 (ctrl + F5) or publishes the app, rdlc is "removed" as source and embedded inside some dll somewhere?

And therefore the rdlc-file is not found, when I am executing the: stream = Me.GetType().Assembly.GetManifestResourceStream(rdlc)
0
gbzhhuCommented:
Having reproduced your type of web site, I see that the report is NOT actually built into a dll but is placed in the folder the project was deployed into as a file.

Basically I created a web site using File/New/Website
I added a report
I built site
I Published site to a location on my PC- say C:\reportTest
The report file appears there and also a a bin folder gets created there and in it is a temporary dll holidng app compiled code.

Retry my steps and if you get the same result then you are better off opening the report file as just a normal file (I can help you with that code) rather than trying to read it from the app dll (which it is not embed for this type of site)

You with me?
0
JoarAuthor Commented:
Yes you ar right. I did say something wrong to you some time ago.

The rdlc files are still rdlc files
0
gbzhhuCommented:
Ok.  So what do you want to do with the report file?  Open it modify xml? or what?
0
JoarAuthor Commented:
Yes exacty, open the file, and modify some elements
If you give me an examplecode, I should be able to the rest

0
gbzhhuCommented:
OK, give me 10 mins.
0
gbzhhuCommented:
Hi Can you post a sample rdlc file and the element/s that you want to change.  Or shall i just post a generic code that opens an xml file and modify a node?
0
JoarAuthor Commented:
Yes  a generic code will do.
I tried some  time ago to open it as a XLM, but got som error.
So I look forward to your way of doing it!

0
gbzhhuCommented:
Ok.  Sorry I lost the VB project but I created a C# one because it is easier for me.  I hope you can translate it to VB.

Here.  opens your report file (need to change name to your report!)  Reads the first InitialDimensions/UnitType which is 'Inch' in my report and changes it to 'WOO' then saves the report file.  try it.

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("report.rdlc"));
        XmlNode root = xDoc.DocumentElement;
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
        nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
        XmlNode unitType = root.SelectSingleNode("rd:InitialDimensions/rd:UnitType", nsmgr);
        unitType.InnerText = "WOO";
        xDoc.Save(Server.MapPath("report.rdlc"));
0
gbzhhuCommented:
You need to import System.Xml obviously.
0
JoarAuthor Commented:
Thank you very much so far!
You have been an excelent support for me!

I will enjoy trying this code during the weekend and report back therafter.
Have a nice weekend until then!
0
gbzhhuCommented:
It is a pleasure.  You have a great weekend too.
0
JoarAuthor Commented:
It functions, but as soon as I try to search elements in my report I am failing, because of my bad knowledge af XLM doc manipulation.

Could you please give me a few advices more before we close down this case?

1. How may I save the modified rdlc to a new file (wouldn't like to crasch me rdlc my accident...)
2. How may I instead return the modified XML-doc as a io.textreader (as would be the function type for ReportViewer1.LocalReport.LoadReportDefinition())?
3. Please indicate how to find the "Width" and "Height" elements  in the folowing code at the end of the attached code snippet (rdlc-file):
              <Image Name="image2">
                <Sizing>FitProportional</Sizing>
                <Left>0.04232cm</Left>
                <Top>1.94444cm</Top>
                <Width>7.03175cm</Width>
                <Source>External</Source>
                <Style />
                <Height>1.63492cm</Height>
                <Value>="file:///" &amp; Parameters!parSign.Value</Value>
              </Image>
 
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <DataSources>
    <DataSource Name="conSolidusR">
      <ConnectionProperties>
        <ConnectString />
        <DataProvider>SQL</DataProvider>
      </ConnectionProperties>
      <rd:DataSourceID>c9a27540-6668-4e02-af62-85ca95415c52</rd:DataSourceID>
    </DataSource>
  </DataSources>
  <BottomMargin>0.5cm</BottomMargin>
  <RightMargin>0.5cm</RightMargin>
  <PageWidth>21cm</PageWidth>
  <ReportParameters>
    <ReportParameter Name="parLogo">
      <DataType>String</DataType>
      <Nullable>true</Nullable>
      <AllowBlank>true</AllowBlank>
      <Prompt>parLogo</Prompt>
    </ReportParameter>
    <ReportParameter Name="parSign">
      <DataType>String</DataType>
      <Nullable>true</Nullable>
      <AllowBlank>true</AllowBlank>
      <Prompt>parSign</Prompt>
    </ReportParameter>
  </ReportParameters>
  <rd:DrawGrid>true</rd:DrawGrid>
  <InteractiveWidth>21cm</InteractiveWidth>
  <rd:GridSpacing>0.25cm</rd:GridSpacing>
  <rd:SnapToGrid>false</rd:SnapToGrid>
  <Body>
    <ColumnSpacing>1cm</ColumnSpacing>
    <ReportItems>
      <List Name="list1">
        <ReportItems>
          <Textbox Name="textbox15">
            <Left>12.38095cm</Left>
            <Top>20.2381cm</Top>
            <rd:DefaultName>textbox15</rd:DefaultName>
            <ZIndex>31</ZIndex>
            <Width>4.49735cm</Width>
            <Style>
              <PaddingLeft>2pt</PaddingLeft>
              <PaddingBottom>2pt</PaddingBottom>
              <PaddingRight>2pt</PaddingRight>
              <PaddingTop>2pt</PaddingTop>
            </Style>
            <CanGrow>true</CanGrow>
            <Height>0.63492cm</Height>
            <Value />
          </Textbox>
          <Rectangle Name="rectangle1">
            <Left>0.05291cm</Left>
            <ReportItems>
              <Textbox Name="SU_SignTittel">
                <Left>0.05291cm</Left>
                <CanShrink>true</CanShrink>
                <Top>4.44445cm</Top>
                <rd:DefaultName>SU_SignTittel</rd:DefaultName>
                <ZIndex>4</ZIndex>
                <Width>7.19577cm</Width>
                <Style>
                  <PaddingLeft>2pt</PaddingLeft>
                  <PaddingBottom>2pt</PaddingBottom>
                  <PaddingRight>2pt</PaddingRight>
                  <PaddingTop>2pt</PaddingTop>
                </Style>
                <CanGrow>true</CanGrow>
                <Height>0.63492cm</Height>
                <Value>=Fields!SU_SignTittel.Value</Value>
              </Textbox>
              <Textbox Name="SU_SignNavn">
                <Left>0.05291cm</Left>
                <Top>3.75662cm</Top>
                <rd:DefaultName>SU_SignNavn</rd:DefaultName>
                <ZIndex>3</ZIndex>
                <Width>7.16931cm</Width>
                <Style>
                  <PaddingLeft>2pt</PaddingLeft>
                  <PaddingBottom>2pt</PaddingBottom>
                  <PaddingRight>2pt</PaddingRight>
                  <PaddingTop>2pt</PaddingTop>
                </Style>
                <CanGrow>true</CanGrow>
                <Height>0.63492cm</Height>
                <Value>=Fields!SU_SignNavn.Value</Value>
              </Textbox>
              <Textbox Name="textbox14">
                <Left>0.05291cm</Left>
                <Top>0.18519cm</Top>
                <rd:DefaultName>textbox14</rd:DefaultName>
                <ZIndex>2</ZIndex>
                <Width>7.43386cm</Width>
                <Style>
                  <PaddingLeft>2pt</PaddingLeft>
                  <PaddingBottom>2pt</PaddingBottom>
                  <PaddingRight>2pt</PaddingRight>
                  <PaddingTop>2pt</PaddingTop>
                </Style>
                <CanGrow>true</CanGrow>
                <Height>0.63492cm</Height>
                <Value>=Fields!Hilsning.Value</Value>
              </Textbox>
              <Textbox Name="BrevGiroTekst">
                <Left>0.02646cm</Left>
                <Top>1.041cm</Top>
                <ZIndex>1</ZIndex>
                <Style>
                  <PaddingLeft>2pt</PaddingLeft>
                  <PaddingBottom>2pt</PaddingBottom>
                  <PaddingRight>2pt</PaddingRight>
                  <PaddingTop>2pt</PaddingTop>
                </Style>
                <CanGrow>true</CanGrow>
                <Height>0.79498cm</Height>
                <Value>=Fields!BrevGiroTekst.Value </Value>
              </Textbox>
              <Image Name="image2">
                <Sizing>FitProportional</Sizing>
                <Left>0.04232cm</Left>
                <Top>1.94444cm</Top>
                <Width>7.03175cm</Width>
                <Source>External</Source>
                <Style />
                <Height>1.63492cm</Height>
                <Value>="file:///" &amp; Parameters!parSign.Value</Value>
              </Image>
      

Open in new window

0
gbzhhuCommented:
1.  Just give a different path like xDoc.Save(Server.MapPath("modifiedReport.rdlc"));

2.  After saving the modifed file you can do

        TextReader tr = new StreamReader(Server.MapPath("modifiedReport.rdlc"));
        return tr;

Make sure the function return is TextReader

3.  I am having trouble reading the Image node.  I will figure it out.

0
JoarAuthor Commented:
1. I did try to save as you said. I see now that it is fysically saved, but not included in the project (I have to do it manually?)
Does not matter if I may read it and return it as a TextReader.  That is what I normally want. Do I have to save it before or could I return the modified versjon of the rdlc on the fly (as a TextReader)?
0
gbzhhuCommented:
Yeah, it won't be in the project.  If you want that you have to do it manually.

I don't know a way of returning TextReader out of XMLDocument without writing to file and then reading.  The StreamReader constructor will only allow a Stream or a path to a file.  What you can do is write file load it to StreamReader then delete it with File.Delete.

I am still struggling reading your Image node.  The problem is that you have a namespace "rd" defined.  We can read the namespaced nodes but not the others.  I do not know exactly why that is happening.  Was the file automatically generated or did you add the namespace?

I got a bit of work to do now but will try again later to read the Image node

0
JoarAuthor Commented:
It's OK with the save and read, no problem.

The rdlc file is created by using th vs 2005 Add/ New Item  Report (no manuel action)

I must say that I do not understand quit the use of namespace in the rdlc-file....
When I look at different reports I have made, I see that the "rd:" is poping up here and there.
But the meaning anf the system is beyond my understanding.

I still hope you will be with me to end...
0
gbzhhuCommented:
Hi,

Don't worry mate.  I will do all I can to help you.

The namespace - this explains it well.  It is an east concept
http://www.xml.com/pub/a/1999/01/namespaces.html

Bear with me.  I am working while trying to help you, so I am going to be a bit slower but I am going to be around till we sort this out :-)
0
gbzhhuCommented:
Ok.  I think I got it this time :-)

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("report.rdlc"));
        XmlNode root = xDoc.DocumentElement;
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
        nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
        nsmgr.AddNamespace("pf", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");
        XmlNode image = root.SelectSingleNode("//pf:Image[@Name='image2']", nsmgr);
        image.InnerText = "WOO";
        xDoc.Save(Server.MapPath("report2.rdlc"));
        TextReader tr = new StreamReader(Server.MapPath("report2.rdlc"));
        return tr;

How does that work?
0
JoarAuthor Commented:
Thank you for both the XML-link which I will study, and the code, but (the last one):
My intention is to modify the  Width and Height values of the image (I tried different syntaxes, but all of them failed).
Could you indicate the change for doing that?
              <Image Name="image2">
                <Sizing>FitProportional</Sizing>
                <Left>0.04232cm</Left>
                <Top>1.94444cm</Top>
                <Width>7.03175cm</Width>
                <Source>External</Source>
                <Style />
                <Height>1.63492cm</Height>
                <Value>="file:///" &amp; Parameters!parSign.Value</Value>
              </Image>
0
gbzhhuCommented:
Ok that is easy

In my previous code replace (and also see my comment to understand hwta I am doing so you can change any other node)

image.InnerText = "WOO";

with
        //We already have a variable image pointing to the image node
        //Width is a child of the image node and so we get it as follows
        XmlNode width = image.SelectSingleNode("pf:Width", nsmgr);
        //We then set it's value after checking it is not null to avoid errors
        if(width != null)
             width.InnerText = "20";
        //We do the same for height
        XmlNode height = image.SelectSingleNode("pf:Height", nsmgr);
        if(height != null)
             height.InnerText = "40";

I will explain this pf: namespace use after we achieve what you want
0
JoarAuthor Commented:
Brilliant!

Yes, I was wondering about the pf:
0
gbzhhuCommented:
So it works Ok?

Let's deal with the pf: thingy!

Basically your rdlc file declares the root node as
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">

What this is saying is that
-  We want to setup 2 namespaces in order to distinguish our nodes

-  This one xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" is for report designer.  To break it up, xmlns is a keyword to declare namespaces.  The : separates the keyword from the namespace name, which is 'rd' in our case.  It could have been called anything but that is what Microsoft decided to call.  The rest in quotes is a URL.  Now this url does not have to exist.  The reson it was decided to use url is because of their uniqueness - there is only one microsoft.com on the Internet

-  This one xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" is for report definition.  M$ knows what the different is between report designer and report definition but we don't have to worry about it.  What you need to notice is that, although the same explanation I gave above applies to this one the report name is missing.  This is called default namespace.  Any nodes which don't have a namespace prefix will take the default namespace.

In the C# code (VB would be the same - only syntax difference) We did this - see comment in the code below

//We create an XmlNamespaceManager, passing our document's nametable - see here for explanation of nametable
http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.nametable.aspx
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);

//We add our namespaces as explained earlier.  The second one 'pf' is the default one.  I could have given it any name really.  You can replace pf in your code with anything you want.  Notice that the url's are exactly as it is defined in the rdlc file.  They must match.
        nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
        nsmgr.AddNamespace("pf", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

Now finally we get the node we want - for example Image
XmlNode image = root.SelectSingleNode("//pf:Image[@Name='image2']", nsmgr);
Since image didn't have any namespace prefixed, we must give it the default one we created so that XPath can find the node.  Depending on what you are looking for you need to construct a valid XPath, like I did "//pf:Image[@Name='image2']" That says find an image with a Name of image2.  Examples of XPath are here
http://msdn2.microsoft.com/en-us/library/ms256086.aspx

and here
http://www.w3schools.com/xpath/xpath_examples.asp

Good luck.  Ask if I can be of more help.




0
gbzhhuCommented:
Correction

the report name is missing

Should read as

the namespace name is missing

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoarAuthor Commented:
Thank you for all patient help!!!
Yes it did work!
You have been to great help and have initiated the startup of my XML knowlegde!

I will read all your explantion above and spend some of my holyday time next week in Spain reading XML-stuff.
I will now close down this session.
Have a nice time and rest a while....
0
gbzhhuCommented:
I am glad I could help.

Where in Spain are you going?  I went to Barcelona a couple of years ago.  I live in England.

Have a great holiday.  No rest for me :-)  Got to keep learning!.
0
JoarAuthor Commented:
Thanks!
A place called Denia 100 km north of Alicante.
I live in Norway, internet is a fantastic place for commucation
0
gbzhhuCommented:
Wow! sounds like a great place.  Alicante is not far from Africa.  The weather should be nice :-)

Being from Norway, have you ever met people from Somalia?  I am originally from Somalia.

Yeah, I agree the Internet is great.  When I am at work and not much to do I come here and try answering questions.  I learn from it and also help others.  It gives satisfaction.

Enjoy your holiday.
0
JoarAuthor Commented:
Never been in Africa, but there many people from Somalia here in Norway...
0
gbzhhuCommented:
Yeah, Somalis are everywhere.  They are just destroying each other back home and flee to any country they can get to as refugees.  I came to England as a refugee 18 years ago.

0
JoarAuthor Commented:
hello again, I am sorry to say your code did get me into a new problem...

Your code functions OK the first time.
Next time I am not allowed to write to the same modified file ( xDoc.Save(Server.MapPath("report2.rdlc"));
I neither is allowed to delete the file with: System.IO.File.Delete(Server.MapPath("report2.rdlc")

Both actions give me the error: The process cannot access file... because it is used by another process.
Several searches on the internet did'nt give me any understandable solution.

     XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("report.rdlc"));
        XmlNode root = xDoc.DocumentElement;
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
        nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
        nsmgr.AddNamespace("pf", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");
        XmlNode image = root.SelectSingleNode("//pf:Image[@Name='image2']", nsmgr);
        image.InnerText = "WOO";
        xDoc.Save(Server.MapPath("report2.rdlc"));
        TextReader tr = new StreamReader(Server.MapPath("report2.rdlc"));
        return tr;

0
gbzhhuCommented:
Hi Joar,

That is because of the returned TextReader.  The line
TextReader tr = new StreamReader(Server.MapPath("report2.rdlc"));
opens the file and returns the TextReader variable tr back to whatever function called this function.  While that TextReader is still open you cannot do anything with the file.  Make sure the returned TextReader is closed before attempting any file operations.

 
0
JoarAuthor Commented:
Thank you. Fixed it!
I had to define the TextReader as a global in order to be able to close it and delete the file after returning from the function modyfing the rdlc-file.
Silence from me now....
0
gbzhhuCommented:
>>I had to define the TextReader as a global
It is not good idea to define variables as global.  They often cause problems later.  Just thought I'd mention that.  The best way is to deal with the TextReader in the function that calls the function that modifies the rdlc-file.  Example

private void SomeFunctionName()
{
       //Create the textreader here and assign the value coming back the FunctionThatModifiesRDLC function
       TextReader tr = FunctionThatModifiesRDLC();

      //Do something with the TextReader tr variable
      .....

      //Now close it
      tr.Close();
}

I know it is working for you now so you can leave it.  I just wanted to mention this.

>>Silence from me now....
Not necessarily :-)  Ask if you get stuck.  It is a pleasure to help.
0
JoarAuthor Commented:
Nice, I will do it that way. I am hganged up in my old ways of doing it.
Thank you againg.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.