C#

XPATH With Default Namespace In C# – XPathNavigator

This article explains the sample C# code with XPathNavigator to select an XML element from an XML file which is having a default namespace.
Consider the sample XML file with namespace(xmlns) defined ,

   
<!--?xml version="1.0" encoding="UTF-8"?--> 
<country xmlns="http://www.countrys.org/2013/5/Country" version="1.2">
   <state name="Maharashtra"> 
         <city>Pune</city> 
         <city>Mumbai </city> 
   </state>   

  <state name="Maharashtra"> 
         <city>Bangalore</city> 
         <city>Mangalore </city> 
   </state> 
</country>

See the console application sample given below which selects all the City Elements of the State which is having Name attribute value ‘Karnataka’.XPathNavigator is used for parsing.

class Program
{
     static void Main(string[] args)
     {

        //Replace the hardcoded XML  file path with your XML file path.
        XPathDocument xpathDocument = new XPathDocument(@"E:\XmlSamples\Country.xml");
        XPathNavigator navigator = xpathDocument.CreateNavigator();

        //Define the Namespace manager
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("ns", "http://www.countrys.org/2013/13/CountrySchema");

        //Set the Xpath expression
        string xPathQuery = "//ns:State[@Name='Karnataka']/ns:City";
        XPathNodeIterator nodes = navigator.Select(xPathQuery, nsmgr);
        foreach (XPathNavigator item in nodes)
        {
             Console.WriteLine(item.InnerXML);
        }
        Console.Read();
    }
}

Since the XML is having a namespace defined in it, your XPath expression need namespace manager which is defined with the 2 lines below.This namespace manager is used in XpathNavigator select.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "http://www.countrys.org/2013/13/CountrySchema");

Notice the XPath expression  //ns:State[@Name=’Karnataka’]/ns:City, here the text ns:(instead of ns, use any variable name as you like) before every element name is needed since the XML file is having a default namespace. Every node name in the expression needs this namespace text.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

1 year ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

1 year ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

1 year ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

1 year ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

1 year ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

1 year ago