SelectNodes with a namespace

T

Trace C

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,
Trace
 
D

Drebin

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))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top