SelectSingleNode when xmlns is an x-schema

  • Thread starter Thread starter Henry Padilla
  • Start date Start date
H

Henry Padilla

I'm sorry if this has been asked and answered, I looked and didn't find this
particular situation so I have to ask.

If I understand correctly, the .NET XMLDOM must have an XmlNamespaceManager
bound even if the namespace doesn't have a name. (i.e. <TopNode
xmlns='www.microsoft.com'>)

And I've tried different configurations, nothing has helped. Here's a
sample XML doc:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<TopLevel xmlns="x-schema:InfoSchema.xml">
<MyInfos>
<MyInfo>
<Type>TYPE_ONE</Type>
<Description>First Type</Description>
</MyInfo>
<MyInfo>
<Type>TYPE_TWO</Type>
<Description>Other Type</Description>
</MyInfo>
</MyInfos>
</TopLevel>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

My code goes something like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XmlDocument XMLDoc = new XmlDocument ();

XMLDoc.Load(XMLFilePath);

XmlNodeList MyInfos = XMLDoc.GetElementsByTagName("MyInfo");

foreach (XmlNode MyInfo in MyInfos)
{
XmlNode MyInfoType = MyInfo.SelectSingleNode("./Type"); //Doesn't Work

XmlNamespaceManager MyNsM = new XmlNamespaceManager(XMLDoc.NameTable);
MyNsM.AddNamespace("default", "x-schema:InfoSchema.xml");


XmlNode MyInfoType = MyInfo.SelectSingleNode("./default:Type");
//Doesn't Work

XmlNode MyInfoType = MyInfo.SelectSingleNode("//Type"); //Doesn't Work

XmlNode MyInfoType = MyInfo.SelectSingleNode("./*"); //Works but it's
not specific
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


How do I get the "Type" when I want it and the "Description" when I want it?

Thanks so much in advance for the help.

Tom P.
 
Henry,

The documentation from AddNamespace states:

Parameters
prefix

The prefix to associate with the namespace being added. Use String.Empty
to add a default namespace.

Hope this helps.
 
Back
Top