Groups | Blog | Home
all groups > c# > march 2004 >

c# : SelectNodes with a namespace



tracey.roy NO[at]SPAM dewr.gov.au
3/29/2004 11:55:14 PM
I have XML that has XML embedded in it, which has a namespace on the
root element I am trying to get, so I end up with XML of:

<ApplicationsList xmlns="urn:sp-schema">
<Application>
<Name></Name>
<ApplicationAddress></ApplicationAddress>
<FormAddress></FormAddress>
<Description></Description>
</Application>
<Application>
<Name></Name>
<ApplicationAddress></ApplicationAddress>
<FormAddress></FormAddress>
<Description>EA3000</Description>
<ImageIndex>0</ImageIndex>
.....

So I load this XML into a DOM, using LoadXml

appListDoc.LoadXml(xmlNode.OuterXml);

//Create the XmlNamespaceManager.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(appListDoc.NameTable);
nsmgr.AddNamespace("", "urn:sp-schema");

foreach (XmlNode appNode in
appListDoc.DocumentElement.SelectNodes("Application", nsmgr))
{
// do stuff
}

In my foreach loop, it never finds the "Application" element. But if
I iterate through the childNodes, all is fine - but I don't want to do
this.

======= Immediate Window ================
appListDoc.DocumentElement.ChildNodes
{System.Xml.XmlChildNodes}
[System.Xml.XmlChildNodes]: {System.Xml.XmlChildNodes}
System.Object: {System.Xml.XmlChildNodes}
Count: 16
ItemOf: <cannot view indexed property>
=========================================

Please could someone help!

Thanks,
Drebin
3/30/2004 12:47:26 PM
I've had better luck by giving the namespace an alias - when the namespace
is the default or only one listed.. something like this:

nsmgr.AddNamespace("default", "urn:sp-schema");

foreach (XmlNode appNode in
appListDoc.DocumentElement.SelectNodes("default:Application", nsmgr))


[quoted text, click to view]

Lee Swanson
3/30/2004 4:03:16 PM
Try using an xpath with the SelectSingleNode method.

Lee.

[quoted text, click to view]

tracey.roy NO[at]SPAM dewr.gov.au
3/31/2004 3:28:48 PM
Drebin

thanks so much, this worked perfect!!

AddThis Social Bookmark Button