In this article I will explain with an example, how to use
XPath with
XmlDocument and how to select
XML nodes by
Name and
Attribute value using
XPath 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>NancyDavolio</employeename>
<country>USA</country>
</employee>
<employee id="2" city="Tacoma">
<employeename>Andrew Fuller</employeename>
<country>USA</country>
</employee>
<employee id="3" city="Kirkland">
<employeename>JanetLeverling</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>MichaelSuyama</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>AnneDodsworth</employeename>
<country>UK</country>
</employee>
</employees>
Namespaces
You will need to import the following namespace.
C#
VB.Net
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(@"E:\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 below screenshot is displaying the InnerText and Attribute values of the nodes selected by Node name.
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(@"E:\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 below screenshot is displaying the InnerText and Attribute values of the nodes selected by Node name.