471,605 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

SelectSingleNode when xmlns is an x-schema

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.
Dec 6 '05 #1
1 2112
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Henry Padilla" <pa******@hotmail.com> wrote in message
news:e0******************@TK2MSFTNGP12.phx.gbl...
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.

Dec 6 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Glenn Venzke | last post: by
2 posts views Thread by Edward Yang | last post: by
7 posts views Thread by Jason | last post: by
1 post views Thread by K. Wilder | last post: by
1 post views Thread by H Irick | last post: by
6 posts views Thread by David Thielen | last post: by
3 posts views Thread by Jonathan | last post: by
reply views Thread by Martin | last post: by
reply views Thread by MichaelMortimer | last post: by

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.