Extract node from XML having defined namespace using XPath in C#

02 Feb

Problem Statement:
At work, while working on one of the features, we had to get a specific node from an XML. Obvious choice was to use XPath. Ignoring specifics of XML, we directly put the XPath as expected and found that no node was found when we executed the path.

I had to look up on how to extract the deep embedded node we needed using XPath in such case.

We observed that the elements/nodes are bound to a namespace. When we tried the direct XPath, it was attempting to address elements that are bound to the default “no namespace” namespace, and thus nothing was retrieved.

Sample XML to work with:

<?xml version="1.0"?>
<SampleXmlRoot xmlns="">
<SampleXmlSubRoot someValue="21" xmlns="">
<SampleXmlLevel2Node id="101" xmlns="">
<SampleXmlNode2Extract num="2">
<A1 type="money">
<A2 type="money">
<A3 type="money">
<SampleXmlNodeOther num="2">
<B1 type="money">
<B2 type="money">

Based on what we observed and quick reference we learned that we need to register the namespace for namespace prefix mapping that will be used in defining the XPath.

Using XmlNamespaceManager object, we can provide a collection of namespace definitions that will be  used by CLR to resolve the elements used in the XML documents.

Step 1: Define XmlNamespaceManager

// xmlDoc is XmlDocument in which full XML is loaded
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);

// Add the namespaces used in XML to the XmlNamespaceManager
xmlnsManager.AddNamespace("sxr", "");
xmlnsManager.AddNamespace("sxsr", "");
xmlnsManager.AddNamespace("sxl2", "");

Step 2: Use select method that is overloaded to use XmlNamespaceManager along with XPath

In our case, we will select the first XmlNode that matches the XPath expression where any prefixes found in the XPath expression will be resolved using the supplied XmlNamespaceManager.

String xPath = "/sxr:SampleXmlRoot/sxsr:SampleXmlSubRoot/sxl2:SampleXmlLevel2Node/SampleXmlNode2Extract";
XmlNode extractedNode = xDoc.SelectSingleNode(xPath, xmlnsManager);

XmlNode extracted OuterXml will look something like:

<SampleXmlNode2Extract num="2">
<A1 type="money">
<A2 type="money">
<A3 type="money">

Yep, that’s the exact one we needed!

MSDN: XmlNamespaceManager Class
MSDN: XmlNode.SelectSingleNode Method (String, XmlNamespaceManager)

XPath is a wonderful way to extract node(s) and we have defined methods in place to get them. We just need to know and learn about them. XmlNamespaceManager handles the namespace and helps in defining the XPath for a given XML.

Leave a comment

Posted by on February 2, 2013 in C#


Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: