What is XPath expression?
Understanding the increasing need of
XML, W3C has developed a new querying language called XPath expression through which we
can easily walk into a XML document and select a node. In simple terms, XPath is
simple and easily understandable expressions which we can use to select XML node
or nodes in an XML document.
To understand this article, we will use
the below simple XML.
<?xml version="1.0"
encoding="utf-8"?>
<Employees>
<Employee
type="Permanent">
<ID>100</ID>
<FirstName>Bala</FirstName>
<LastName>Murugan</LastName>
<Dept>Production
Support</Dept>
</Employee>
<Employee
type="Contract">
<ID>101</ID>
<FirstName>Peter</FirstName>
<LastName>Laurence</LastName>
<Dept>Development</Dept>
</Employee>
<Employee
type="Permanent">
<ID>102</ID>
<FirstName>Rick</FirstName>
<LastName>Anderson</LastName>
<Dept>Sales</Dept>
</Employee>
<Employee
type="Contract">
<ID>103</ID>
<FirstName>Ramesh</FirstName>
<LastName>Kumar</LastName>
<Dept>HR</Dept>
</Employee>
<Employee
type="Permanent">
<ID>104</ID>
<FirstName>Katie</FirstName>
<LastName>Yu</LastName>
<Dept>Recruitment</Dept>
</Employee>
<Employee
type="Contract">
<ID>105</ID>
<FirstName>Suresh</FirstName>
<LastName>Babu</LastName>
<Dept>Inventory</Dept>
</Employee>
</Employees>
XPath expression Syntax
The XPath expression will use / to
select the root node of the XML document. For example, to select all the
employee nodes in the above XML we have to use the below XPath query,
/Employees/Employee
The above XPath query will return all
nodes under every <Employee> nodes.
The below table list some of the
important expressions to build an XPath query.
Expression |
Description |
node |
To select all child nodes under the
node “node” |
/ |
To select the root
node |
// |
To select the node from any where in
the document. |
. |
To select the current
node |
.. |
To select the parent of the current
node |
@ |
To select
attributes |
Moving forward, we will see how these
XPath expressions can be used in .net world to access and manipulate XML
contents.
In this section, we will see how to
read a XML node at a very basic level using XmlDocument class System.Xml
namespace. We can use SelectSingleNode() and SelectNodes() methods of
XmlDocument class to query the XML using XPath expression. See below,
To select a single node, for example
first name,
XmlDocument xmldoc = new
XmlDocument();
xmldoc.Load(Server.MapPath("~/App_Data/Employees.xml"));
Response.Write(xmldoc.SelectSingleNode("/Employees/Employee/FirstName").InnerText);
OUTPUT
Bala
To select nth occurence of a node
value, for example to get 3rd employee’s firstname,
XmlDocument xmldoc = new
XmlDocument();
xmldoc.Load(Server.MapPath("~/App_Data/Employees.xml"));
Response.Write(xmldoc.SelectSingleNode("/Employees/Employee[3]/FirstName").InnerText);
OUTPUT
Rick
To select a node conditionally, for
example to select firstname of a employee with ID as 101,
XmlDocument xmldoc = new
XmlDocument();
xmldoc.Load(Server.MapPath("~/App_Data/Employees.xml"));
Response.Write(xmldoc.SelectSingleNode("/Employees/Employee[ID=101]/FirstName").InnerText);
OUTPUT
Peter
To select a attribute of a XML node,
for example to get the type of employee with ID 101,
XmlDocument xmldoc = new
XmlDocument();
xmldoc.Load(Server.MapPath("~/App_Data/Employees.xml"));
Response.Write(xmldoc.SelectSingleNode("/Employees/Employee[ID=101]/@type").InnerText);
OUTPUT
Contract
To get a count of all permanent
employees,
XmlDocument xmldoc = new
XmlDocument();
xmldoc.Load(Server.MapPath("~/App_Data/Employees.xml"));
Response.Write(xmldoc.SelectNodes("/Employees/Employee[@type='Permanent']").Count.ToString());
OUTPUT
3
To get all employee count,
XmlDocument xmldoc = new
XmlDocument();
xmldoc.Load(Server.MapPath("~/App_Data/Employees.xml"));
Response.Write(xmldoc.SelectNodes("//Employee").Count.ToString());
OUTPUT
6
|