Basic Queries (LINQ to XML)

This section provides examples of basic LINQ to XML queries. 

In This Section

Topic

Description

How to: Find an Element with a Specific Attribute

Shows how to find a particular element that has an attribute that has a specific value.

How to: Find an Element with a Specific Child Element

Shows how to find a particular element that has a child element that has a specific value.

Querying an XDocument vs. Querying an XElement

Explains the differences between writing queries on an XML tree that is rooted in XElement and writing queries on an XML tree that is rooted in XDocument.

How to: Find Descendants with a Specific Element Name

Shows how to find all the descendants of an element that have a specific name. This example uses the Descendants axis.

How to: Find a Single Descendant Using the Descendants Method

Shows how to use the Descendants axis method to find a single uniquely named element.

How to: Write Queries with Complex Filtering

Shows how to write a query with a more complex filter.

How to: Filter on an Optional Element

Shows how to find nodes in an irregularly shaped tree.

How to: Find All Nodes in a Namespace

Shows how to find all nodes that are in a specific namespace.

How to: Sort Elements

Shows how to write a query that sorts its results.

How to: Sort Elements on Multiple Keys

Shows how to sort on multiple keys.

How to: Calculate Intermediate Values

Shows how to use the Let clause to calculate intermediate values in a LINQ to XML query.

How to: Write a Query that Finds Elements Based on Context

Shows how to select elements based on other elements in the tree.

How to: Debug Empty Query Results Sets

Shows the appropriate fix when debugging queries on XML that is in a default namespace.

See Also

Concepts

Querying XML Trees