Selecting nodes when namespaces are involved
Posted by Chris Alcock on Sunday 7th January 2007 at 06:41 pm | Tagged as: .NET, Development, XML
I needed to select out XML nodes from an XML document that had namespaces attached and wasn’t getting the results I needed from my real code, so using snippet compiler I cooked up a simple example to get it working. Presented below is that simple example, here more as a reminder to myself if I need it again.
using System;
using System.Xml;
public class XPathWithNamespaces
{
public static void Main()
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(""
+"
test ");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xd.NameTable);
nsmgr.AddNamespace("foo", "http://foo.bar/mouse");
XmlNode xn = xd.SelectSingleNode("//foo:theone", nsmgr);
if (xn != null)
{
Console.WriteLine(xn.Name);
}
Console.ReadLine();
}
}
Comments Off on Selecting nodes when namespaces are involved