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 |
---|---|
Provides an overview of the similarities and differences between XPath and LINQ to XML. |
|
Compares the XPath child element axis to the LINQ to XML Element method. The associated XPath expression is:"DeliveryNotes". |
|
Compares the XPath child elements axis to the LINQ to XML Elements axis. The associated XPath expression is:"./*" |
|
Compares how to get the root element with XPath and LINQ to XML. The associated XPath expression is:"/PurchaseOrders" |
|
Compares how to get the descendant elements with a particular name with XPath and LINQ to XML. The associated XPath expression is:"//Name" |
|
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']" |
|
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]" |
|
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:*" |
|
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" |
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" |
|
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]" |