Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting from an XmlNode using XmlNamespaceManager

Tags:

c#

.net

xml

I have been looking for ages to find a way to select nodes from an XmlNode(NOT AN XmlDocument) which has multiple namespaces.

Almost every post that I have searched has advised me to use an XmlNamespaceManager, however, XmlNamespaceManager needs an XmlNameTable which does not exists for an XmlNode.

I tried doing this with an XmlDocument and it worked since XmlDocument has a property XmlDocument.NameTable but it does not exists for XmlNode.

I tried creating a NameTable manually but it does not work as the same piece of code works when I use an XmlDocument. I guess I need to populate that NameTable with something or somehow bind it to the XmlNode to make this work. Please suggest.

like image 224
thisdotnull Avatar asked Mar 02 '26 02:03

thisdotnull


1 Answers

Can you use

XPathNavigator nav = XmlNode.CreateNavigator();
XmlNamespaceManager man = new XmlNamespaceManager(nav.NameTable);

Including the rest in case it'll be helpful:

man.AddNamespace("app", "http://www.w3.org/2007/app"); //Gotta add each namespace
XPathNodeIterator nodeIter = nav.Select(xPathSearchString, man);

while (nodeIter.MoveNext())
{
    var value = nodeIter.Current.Value;
}

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.createnavigator.aspx

like image 81
CeeRo Avatar answered Mar 04 '26 15:03

CeeRo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!