LINQ to XML for XPath Users

This set of topics show a number of XPath expressions and their LINQ to XML equivalents. 

All of the examples use the XPath functionality in LINQ to XML that is made available by the extension methods in System.Xml.XPath.Extensions. The examples execute both the XPath expression and the LINQ to XML expression. Each example then compares the results of both queries, validating that the XPath expression is functionally equivalent to the LINQ to XML query. As both types of queries return nodes from the same XML tree, the query result comparison is made using referential identity.

In This Section

Topic

Description

Comparison of XPath and LINQ to XML

Provides an overview of the similarities and differences between XPath and LINQ to XML.

How to: Find a Child Element (XPath-LINQ to XML)

Compares the XPath child element axis to the LINQ to XML Element method. 

The associated XPath expression is:"DeliveryNotes".

How to: Find a List of Child Elements (XPath-LINQ to XML)

Compares the XPath child elements axis to the LINQ to XML Elements axis.

The associated XPath expression is:"./*"

How to: Find the Root Element (XPath-LINQ to XML)

Compares how to get the root element with XPath and LINQ to XML.

The associated XPath expression is:"/PurchaseOrders"

How to: Find Descendant Elements (XPath-LINQ to XML)

Compares how to get the descendant elements with a particular name with XPath and LINQ to XML.

The associated XPath expression is:"//Name"

How to: Filter on an Attribute (XPath-LINQ to XML)

Compares how to get the descendant elements with a specified name, and with an attribute with a specified value with XPath and LINQ to XML.

The associated XPath expression is:".//Address[@Type='Shipping']"

How to: Find Related Elements (XPath-LINQ to XML)

Compares how to get an element selecting on an attribute that is referred to by the value of another element with XPath and LINQ to XML.

The associated XPath expression is:".//Customer[@CustomerID=/Root/Orders/Order[12]/CustomerID]"

How to: Find Elements in a Namespace (XPath-LINQ to XML)

Compares the use of the XPath XmlNamespaceManager class with the LINQ to XML Namespace property of the XName class for working with XML namespaces.

The associated XPath expression is:"./aw:*"

How to: Find Preceding Siblings (XPath-LINQ to XML)

Compares the XPath preceding-sibling axis to the LINQ to XML child XNode.ElementsBeforeSelf axis. 

The associated XPath expression is:"preceding-sibling::*"

How to: Find Descendants of a Child Element (XPath-LINQ to XML)

Compares how to get the descendant elements of a child element with a particular name with XPath and LINQ to XML.

The associated XPath expression is:"./Paragraph//Text/text()"

How to: Find a Union of Two Location Paths (XPath-LINQ to XML)

Compares the use of the union operator, |, in XPath with the Concat<TSource> standard query operator in LINQ to XML.

The associated XPath expression is:"//Category|//Price"

How to: Find Sibling Nodes (XPath-LINQ to XML)

Compares how to find all siblings of a node that have a specific name with XPath and LINQ to XML.

The associated XPath expression is:"../Book"

How to: Find an Attribute of the Parent (XPath-LINQ to XML)

Compares how to navigate to the parent element and find an associated attribute using XPath and LINQ to XML.

The associated XPath expression is:"../@id"

How to: Find Attributes of Siblings with a Specific Name (XPath-LINQ to XML)

Compares how to find specific attributes of the siblings of the context node with XPath and LINQ to XML.

The associated XPath expression is:"../Book/@id"

How to: Find Elements with a Specific Attribute (XPath-LINQ to XML)

Compares how to find al elements containing a specific attribute using XPath and LINQ to XML.

The associated XPath expression is:"./*[@Select]"

How to: Find Child Elements Based on Position (XPath-LINQ to XML)

Compares how to find an element based on its relative position using XPath and LINQ to XML.

The associated XPath expression is:"Test[position() >= 2 and position() <= 4]"

How to: Find the Immediate Preceding Sibling (XPath-LINQ to XML)

Compares how to find the immediate preceding sibling of a node using XPath and LINQ to XML.

The associated XPath expression is:"preceding-sibling::*[1]"

See Also

Concepts

Querying XML Trees

Process XML Data Using the XPath Data Model

Reference

System.Xml.XPath