Read XML Data With Namespce Using XmlDocument


In this article we will discuss how to read XML data using XmlDocument object. We can read data through SelectNodes() method which returns XMlNodeList object and it contains list of nodes as per xpath string. However if XML data contains namespace then normal code won't work. We need to create XmlNamespaceManager object and inject with XmlDocument object.

We can also read XML data through LINQ. If you are curious to know how to fetch XML data through LINQ then visit here.

Using Code

Let's define XML data which contains namespace attribute. In the following XML data, Body tag contains namespace attribute as xmlns which contains value as:

"" .

  1. string xmlContent = @"<?xml version='1.0' encoding='utf-8'?>      
  2.    <Body xmlns=''>      
  3.       <Site Id='1'>      
  4.          <Url></Url>      
  5.          <Description>A social community for developers and programmers.</Description>      
  6.       </Site>      
  7.       <Site Id='2'>      
  8.          <Url></Url>      
  9.          <Description>Itwas formed to provide developers with a place to meet.</Description>      
  10.       </Site>      
  11.    </Body>";   
The following code-snippet defines to read XML data. Firstly, it creates XmlDocument object and load xml content to it through LoadXml() method. Secondly, it creates XmlNamespaceManager and add namespace using AddNamespace() method. Thirdly, it injects NamespaceManger object with SelectNodes() method and returns XmlNodeList object. Lastly it loops over the nodelist and get all required values. Result is shown in Figure 1.
  1. XmlDocument doc = new XmlDocument();    
  2. doc.LoadXml(xmlContent);    
  4. // Creating namespace object    
  5. XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);    
  6. nsmgr.AddNamespace("ns""");    
  8. // Fteching nodelist with injecting XML namespace object    
  9. XmlNodeList xNode = doc.SelectNodes("ns:Body/ns:Site", nsmgr);    
  11. foreach (XmlNode xndNode in xNode)    
  12. {    
  13.     string Id = xndNode.Attributes["Id"].InnerText;    
  14.     Console.WriteLine("Id: " + Id);    
  16.     string Url = xndNode["Url"].InnerText;    
  17.     Console.WriteLine("Url: " + Url);    
  19.     string Description = xndNode["Description"].InnerText;    
  20.     Console.WriteLine("Description: " + Description);    
  21. }   

Result of reading data from XML
Figure 1: Result of reading data from XML

Suppose there is a requirement that you want to fetch site details whose Id is 1. In the following code we need to change only in our filtering string(xpath string) - "[@Id='1']" is added.
  1. XmlNodeList xnList = doc.SelectNodes("ns:Body/ns:Site[@Id='1']", nsmgr);    
  2. foreach (XmlNode xn in xnList)    
  3. {    
  4.     string Url = xn["Url"].InnerText;    
  5.     string Description = xn["Description"].InnerText;    
  6. }    

In this blog we discussed how to read XML data when it contains namespace. Also we discussed how to query/filter XML data using XmlDocument object. 

Up Next
    Ebook Download
    View all
    View all