How To Use XPath To Select XML Nodes – C#

XPath (XML Path Language) is based on a DOM representation of an XML document. Therefore, you can use XPath to find a specific node or nodes in an XML file that match some criteria defined in the XPath expression. The article considers С# examples of selecting the required information from an XML file using XPath queries. You will learn how to navigate through an XML document and select nodes using XPath.

To learn how to use the Evaluate() method to navigate an HTML document and select nodes using an XPath query, please see the article How to use the XPath – Evaluate() method.

You can download the data files and complete C# examples that demonstrate the use of the Evaluate() method for XPath Queries from GitHub.

XPath Query – Select Nodes from XML file

These examples will show how to select the required information from an XML file using the XPath query language. Let’s take a look at the XML document cars.xml. This test XML document contains a database of car dealers, their phone numbers, names, and a list of the cars they own.

Let’s sequentially compose XPath expressions that will select names, phone numbers and a list of cars of interest to you. XPath is powerful, as you will see in the following examples:

Select all “Dealer” nodes in the XML file

XPath is used programmatically to evaluate expressions and pick specific nodes in an XML document. To select nodes from XML, use the Evaluate() method.

Let’s start with a straightforward XPath Query for all “Dealer” nodes in the XML document. The following uses the XPath expression //Dealer. It selects all Dealer elements no matter where they are in the document:

C# code

1var dealers = doc.Evaluate("//Dealer", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null);

JavaScript code

1var dealers = document.evaluate("//Dealer", document, null, XPathResult.ANY_TYPE, null);

So, you select information about all dealers from the XML file without any restrictions. XPath expression //Dealer allows you to pick all nested nodes named “Dealer”.

Select Nodes using XPath Axes – descendant::

XPath Query with Limits on Car Production Date

Let’s complicate the XPath query and add a restriction on the year of car production.

You can use XPath Axes in XPath expressions. XPath Axes represents a relationship to the current node and is used to locate nodes relative to that node on the document tree. The descendant axis indicates all of the children of the context node, all of their children, and so forth. In other words, the descendant selects all descendants (children, grandchildren, etc.) of the current node. For example, descendant::Car selects all Car descendants of the current node.

So, let’s create an XPath expression to find the dealers that have a car descendant::Car with a model’s date of manufacture newer than 2005 descendant::Model > 2005. In terms of the DOM tree, we are looking for all “Dealer” children named “Car” that have a child named “Model” that has a value bigger than “2005”. The following uses the XPath expression //Dealer[descendant::Car[descendant::Model > 2005]].

C# code

1var dealers = doc.Evaluate("//Dealer[descendant::Car[descendant::Model > 2005]]", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null);

JavaScript code

1var dealers = document.evaluate("//Dealer[descendant::Car[descendant::Model > 2005]]", document, null, XPathResult.ANY_TYPE, null);

XPath Query with Limits on Car Production Date and Price

In the next step let’s add more restrictions: introduce a restriction on the price of the car and descendant::Price < 25000.

XPath expression is following //Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]. Note: the conditions for price and year of manufacture are combined with and, which means that both of these conditions be performed at the same time:

C# code

1var dealers = doc.Evaluate("//Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null);

JavaScript code

1var dealers = document.evaluate("//Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]", document, null, XPathResult.ANY_TYPE, null);

As a result, only dealers will be selected if they have a car with a manufacture date no older than 2005 and a price of less than 25,000.

C# Example, part 1 – Select Nodes from XML file

Let’s consider how to select dealers from an XML file that have a car with a manufacture date no older than 2005 and a price of less than 25,000, and print the required information to the console. You should follow a few steps:

  1. Load an existing XML file ( cars.xml).
  2. Use the Evaluate() method of the Document class and pass XPath expression //Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]and other parameters to it.
  3. Iterate over the resulted nodes and print their contents to the console.
  4. You will get a list of dealers with the entire content of the “Dealer” nodes.
 1// Create an instance of a document
 2using (var doc = new HTMLDocument(Path.Combine(DataDir, "cars.xml")))
 3{
 4    // Select dealers that match XPath expression
 5    var dealers = doc.Evaluate("//Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null);
 6    Node dealer;
 7
 8    // Iterate over the selected dealers
 9    while ((dealer = dealers.IterateNext()) != null)
10    {
11        Console.WriteLine(dealer.TextContent);
12    }
13}

C# Example, part 2 – Pick Specific Information from Selected Nodes

In the previous part of the example, the XPath query selects all the contents of the “Dealer” node that matched the XPath expression //Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]. But you can choose and print only the information you are interested in from the entire contents of the selected “Dealer”.

In this example ( cars.xml), an internal query was added within the loop to the selected “Dealer” nodes. The query collects as a string the information about the dealer such as the “Name” and “Telephone”: concat('Dealer name: ', Name, 'Telephone: ', Telephone)". Pay attention to the second parameter passed to the Evaluate(expression, contextNode, resolver, type, result) method. This is the node relative to which the query will be executed – the current “Dealer” node. This query concatenates content from nodes with the names “Name” and “Telephone”. Note: the query uses the expected result type of the string XPathResultType.String, and the result value is obtained through the StringValue property and not through the node iterator.

To the list of the selected dealers, you can add the “CarID” attribute, which points to a specific car. A query should be made with the XPath expression .//Car[descendant::Model > 2005 and descendant::Price < 25000]/@CarID. There are several features in the added query; let’s look at them:

 1// Create an instance of a document
 2using (var doc = new HTMLDocument(Path.Combine(DataDir, "cars.xml")))
 3{
 4    // Select dealers that match XPath expression
 5    var dealers = doc.Evaluate("//Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null);
 6    Node dealer;
 7
 8    // Iterate over the selected dealers
 9    while ((dealer = dealers.IterateNext()) != null)
10    {
11        // Get and print Dealer name and Telephone
12        var dealerInfo = doc.Evaluate("concat('Dealer name: ', Name/text(), ' Telephone: ', Telephone/text())", dealer, doc.CreateNSResolver(doc), XPathResultType.String, null);
13        Console.WriteLine(dealerInfo.StringValue);
14
15        // Select and print CarID that match XPath expression
16        var carIds = doc.Evaluate(".//Car[descendant::Model > 2005 and descendant::Price < 25000]/@CarID", dealer, doc.CreateNSResolver(doc), XPathResultType.Any, null);
17        Node carId;
18
19        while ((carId = carIds.IterateNext()) != null)
20        {
21            Console.WriteLine("Car id: " + carId.TextContent);
22        }
23    }
24}

As a result, we get a list of dealers with Names, Telephones and ID codes of the cars that interest us.

Aspose.HTML offers free HTML Web Applications that are an online collection of converters, mergers, SEO tools, HTML code generators, URL tools, and more. The applications work on any operating system with a web browser and do not require any additional software installation. It’s a fast and easy way to efficiently and effectively solve your HTML-related tasks.

Text “HTML Web Applications”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.