Search This Blog

Tuesday, December 24, 2013

Why SelectSingleNode always returns null?

C# > XML > XmlNamespaceManager > SelectSingleNode null

Why SelectSingleNode always returns null?

Because is missing the XML namespace defined SelectSingleNode call.

Solution:

XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);

XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("ns", "urn:www.data.com/feed");

var node = doc.SelectSingleNode("ns:ParentNode/ns:ChildNode", nsMgr);