473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

XPathNavigation and Namespaces.

I have the following code that is attempting to read nodes from an xml
file...

XmlDocument xpdStatus = new XmlDocument();
xpdStatus.Load("status.xml");
XPathNavigator xpnStatus = xpdStatus.CreateNavigator();

XPathNodeIterator xpniStatus = xpnStatus.Select("descendant::Status");
while (xpniStatus.MoveNext())
{
Console.WriteLine("Book title: {0}", xpniStatus.Current.Value);
XmlNode node = ((xpniStatus.Current) as IHasXmlNode).GetNode();
Console.WriteLine("Status: {0}", node.Value);
}

The code works fine until I attempt to add namespaces, with prefixes, into
the XML document, then the xpnStatus.Select never returns the Status nodes.
I've also tried xpnStatus.Select("descendant::Status",
"http:///www.mydomain.net/mynamespace") and xpnStatus.Select("Status",
"http:///www.mydomain.net/mynamespace") to no avail.

Any thoughts?

Here is a snipet of the XML...
<?xml version="1.0" encoding="utf-8"?>
<document xmlns:MNS="http:///www.mydomain.net/mynamespace")>
<MNS:Status guid="8f8ecffd-a692-493c-91d9-0074b74ca56b" damage="false"
reset="false" save="true" show="true">
<MNS:Name>
<MNS:Name>Status1</MNS:Name>
<MNS:Alias>Bl</MNS:Alias>
</MNS:Name>
<MNS:Description><![CDATA[Random description.]]></MNS:Description>
</MNS:Status>
<MNS:Status guid="b7273a52-d346-472c-8506-166f94ca896e" damage="false"
reset="true" save="true" show="false">
<MNS:Name>
<MNS:Name>Status2</MNS:Name>
<MNS:Alias>Cp</MNS:Alias>
</MNS:Name>
<MNS:Description><![CDATA[]]></SRD:Description>
</SRD:Status>
</document>
Nov 12 '05 #1
1 1962
You have to populate an XmlNamespaceManager and pass it to the navigator
Select method.

"Hollywood" <ho*******@thzero.com> wrote in message
news:O1*************@TK2MSFTNGP09.phx.gbl...
I have the following code that is attempting to read nodes from an xml
file...

XmlDocument xpdStatus = new XmlDocument();
xpdStatus.Load("status.xml");
XPathNavigator xpnStatus = xpdStatus.CreateNavigator();

XPathNodeIterator xpniStatus = xpnStatus.Select("descendant::Status");
while (xpniStatus.MoveNext())
{
Console.WriteLine("Book title: {0}", xpniStatus.Current.Value);
XmlNode node = ((xpniStatus.Current) as IHasXmlNode).GetNode();
Console.WriteLine("Status: {0}", node.Value);
}

The code works fine until I attempt to add namespaces, with prefixes, into
the XML document, then the xpnStatus.Select never returns the Status nodes. I've also tried xpnStatus.Select("descendant::Status",
"http:///www.mydomain.net/mynamespace") and xpnStatus.Select("Status",
"http:///www.mydomain.net/mynamespace") to no avail.

Any thoughts?

Here is a snipet of the XML...
<?xml version="1.0" encoding="utf-8"?>
<document xmlns:MNS="http:///www.mydomain.net/mynamespace")>
<MNS:Status guid="8f8ecffd-a692-493c-91d9-0074b74ca56b" damage="false"
reset="false" save="true" show="true">
<MNS:Name>
<MNS:Name>Status1</MNS:Name>
<MNS:Alias>Bl</MNS:Alias>
</MNS:Name>
<MNS:Description><![CDATA[Random description.]]></MNS:Description>
</MNS:Status>
<MNS:Status guid="b7273a52-d346-472c-8506-166f94ca896e" damage="false"
reset="true" save="true" show="false">
<MNS:Name>
<MNS:Name>Status2</MNS:Name>
<MNS:Alias>Cp</MNS:Alias>
</MNS:Name>
<MNS:Description><![CDATA[]]></SRD:Description>
</SRD:Status>
</document>

Nov 12 '05 #2

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
24
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
2
by: Mike Morse | last post by:
What see sample that show xs:element where the xs namespace = http://www.w3.org/2001/XMLSchema However, I see another example with xsi: where xsi = http://www.w3.org/2001/XMLSchema-instance ...
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.