In this article I will explain with an example, how to read (parse) XML using XPath Query (Expression) in C# and VB.Net.
This article will explain how to use XPath to read (parse) XML and select XML nodes by Name and Attribute value using XPath Query (Expression) in C# and VB.Net.
 
 
XML
The following XML file will be used to illustrate the use of XPath with XmlDocument and how to select XML nodes by Name and Attribute value using XPath in C# and VB.Net.
You can download the following XML using the download link provided below.
 
<?xml version="1.0" standalone="yes"?>
<Employees>
    <Employee Id="1" City="Seattle">
        <EmployeeName>Nancy Davolio</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="2" City="Tacoma">
        <EmployeeName>Andrew Fuller</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="3" City="Kirkland">
        <EmployeeName>Janet Leverling</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="4" City="Redmond">
        <EmployeeName>Margaret Peacock</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="5" City="London">
        <EmployeeName>Steven Buchanan</EmployeeName>
        <Country>UK</Country>
    </Employee>
    <Employee Id="6" City="London">
        <EmployeeName>Michael Suyama</EmployeeName>
        <Country>UK</Country>
    </Employee>
    <Employee Id="7" City="London">
        <EmployeeName>Robert King</EmployeeName>
        <Country>UK</Country>
    </Employee>
    <Employee Id="8" City="Seattle">
        <EmployeeName>Laura Callahan</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="9" City="London">
        <EmployeeName>Anne Dodsworth</EmployeeName>
        <Country>UK</Country>
    </Employee>
</Employees>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Xml;
 
VB.Net
Imports System.Xml
 
 
XmlDocument XPath example to select XML nodes by Name in C# and VB.Net
The following example explains how to use XPath with XmlDocument and select XML nodes by name. The XML file is loaded into an XmlDocument and then using XPath all the nodes where Country is USA are fetched as XmlNodeList.
Finally a loop is executed and the InnerText and Attribute values of the selected nodes are printed.
C#
//Load the XML file in XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Xml\Employees.xml");
 
//Fetch the specific Nodes by Name.
XmlNodeList nodeList = doc.SelectNodes("/Employees/Employee[Country='USA']");
 
//Loop through the selected Nodes.
foreach (XmlNode node in nodeList)
{
    //Fetch the Node and Attribute values.
    Console.WriteLine("Name: " + node["EmployeeName"].InnerText + " City: " + node.Attributes["City"].Value);
}
 
VB.Net
'Load the XML file in XmlDocument.
Dim doc As New XmlDocument()
doc.Load("D:\Xml\Employees.xml")
 
'Fetch the specific Nodes by Name.
Dim nodeList As XmlNodeList = doc.SelectNodes("/Employees/Employee[Country='USA']")
 
'Loop through the selected Nodes.
For Each node As XmlNode In nodeList
    'Fetch the Node and Attribute values.
    Console.WriteLine("Name: " + node("EmployeeName").InnerText + " City: " + node.Attributes("City").Value)
Next
 
The following screenshot is displaying the InnerText and Attribute values of the nodes selected by Node name.
Read (Parse) XML using XPath in C# and VB.Net
 
 
XmlDocument XPath example to select XML nodes by Attribute value in C# and VB.Net
The following example explains how to use XPath with XmlDocument and select XML nodes by Attribute values. The XML file is loaded into an XmlDocument and then using XPath all the nodes where City attribute has value London are fetched as XmlNodeList.
Finally a loop is executed and the InnerText and Attribute values of the selected nodes are printed.
C#
//Load the XML file in XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Xml\Employees.xml");
 
//Fetch the specific Nodes by Attribute value.
XmlNodeList nodeList = doc.SelectNodes("/Employees/Employee[@City='London']");
 
//Loop through the selected Nodes.
foreach (XmlNode node in nodeList)
{
    //Fetch the Node and Attribute values.
    Console.WriteLine("Name: " + node["EmployeeName"].InnerText + " City: " + node.Attributes["City"].Value);
}
 
VB.Net
'Load the XML file in XmlDocument.
Dim doc As New XmlDocument()
doc.Load("D:\Xml\Employees.xml")
 
'Fetch the specific Nodes by Attribute value.
Dim nodeList As XmlNodeList = doc.SelectNodes("/Employees/Employee[@City='London']")
 
'Loop through the selected Nodes.
For Each node As XmlNode In nodeList
    'Fetch the Node and Attribute values.
     Console.WriteLine("Name: " + node("EmployeeName").InnerText + " City: " + node.Attributes("City").Value)
Next
 
The following screenshot is displaying the InnerText and Attribute values of the nodes selected by Attribute name.
Read (Parse) XML using XPath in C# and VB.Net