473,508 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read an XML file that contains a namespace?

..NETters,

This may be a simple question. However, I cannot figure out what I am
doing wrong. Would appreciate if someone can help me.

Here is my Sample.xml:

<Report>
</Report>

Here is my code to read it:

XmlDocument doc = new XmlDocument();
doc.Load("Sample.xml");

XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile("Report");

XPathNodeIterator iter = nav.Select(expr);
while(iter.MoveNext()) {
Console.WriteLine(iter.Current.Name);
}

In this case, the output is what I expect.

Now, I modified my xml file and added a namespace as follows:
<Report
xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition">
</Report>

My earlier code does not work after this point. The node iterator does
not return any matching nodes. This is expected.

To fix this, I added the namespace manager object as follows:

XmlDocument doc = new XmlDocument();
doc.Load("Sample.xml");

XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile("Report");

// BEGIN INSERT NEW CODE
XmlNamespaceManager mngr = new XmlNamespaceManager(doc.NameTable);
mngr.AddNamespace("",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");
expr.SetContext(mngr);
// END INSERT NEW CODE

XPathNodeIterator iter = nav.Select(expr);
while(iter.MoveNext()) {
Console.WriteLine(iter.Current.Name);
}

The node iterator still does not return any matching nodes.

Can someone please enlighten me on what is my mistake?

Thank you in advance for your help.

Pradeep

Oct 15 '06 #1
9 1911
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #2
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #3
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #4
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #5
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #6
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #7

Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #8
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #9
Hello Pradeep,

You need to use a prefix when querying xml with xpath even if the xml is in the default namespace.

Try changing the following code...

XPathExpression expr = nav.Compile("/default:Report");

mngr.AddNamespace("default",
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

That should hopefully work now. I'm assuming the Report element is the root of the document as your xml wasn't displayed in your post.
---
Posted via DotNetSlackers.com
Oct 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
13255
by: BCC | last post by:
Hi, I have a tab separated value table like this: header1 header2 header3 13.455 55.3 A string 4.55 5.66 Another string I want to load this guy...
2
3567
by: Dave Griffin | last post by:
This is a followup to my question in doing unmanaged DLLs in .NET. I was able to create the DLL and the application was able to see it. So far so good. Now I need to make that unmanaged DLL read an...
1
3537
by: DjordjeD | last post by:
I need to call(reference) class member "PrintDestination.PrintDestinationPrinter" from another file. How do I do it, I tried "ReportRenderer.PrintDestination.PrintDestinationPrinter" but it...
1
5312
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
4
2585
by: Antonio Tirado | last post by:
Hi, I'm using the OleDB namespace to open a CSV File. I can read the file sucessfully except for hyphenated values. My CSV File contains phone numbers and sometimes these come separated with...
9
4256
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. Is it programatically possible in VB.NET to read the contents of web.config's <customErrorselement? I looked at using ConfigurationSettings.AppSettings, but that doesn't work. I need to...
5
18752
by: sheriff | last post by:
Dear friends, im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am...
9
2968
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
5
11217
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.