LINQ to XML Axes Overview
After you have created an XML tree or loaded an XML document into an XML tree, you can query it to find elements and attributes and retrieve their values. You retrieve collections through the axis methods, also called axes. Some of the axes are methods in the XElement and XDocument classes that return IEnumerable collections. Some of the axes are extension methods in the Extensions class. The axes that are implemented as extension methods operate on collections, and return collections.
As described in XElement Class Overview, an XElement object represents a single element node. The content of an element can be complex (sometimes called structured content), or it can be a simple element. A simple element can be empty or can contain a value. If the node contains structured content, you can use the various axis methods to retrieve enumerations of descendant elements. The most commonly used axis methods are Elements and Descendants.
In addition to the axis methods, which return collections, there are two more methods that you will commonly use in LINQ to XML queries. The Element method returns a single XElement. The Attribute method returns a single XAttribute.
For many purposes, LINQ queries provide the most powerful way to examine a tree, extract data from it, and transform it. LINQ queries operate on objects that implement IEnumerable, and the LINQ to XML axes return IEnumerable of Xelement collections, and IEnumerable of XAttribute collections. You need these collections to perform your queries.
In addition to the axis methods that retrieve collections of elements and attributes, there are axis methods that allow you to iterate through the tree in great detail. For example, instead of dealing with elements and attributes, you can work with the nodes of the tree. Nodes are a finer level of granularity than elements and attributes. When working with nodes, you can examine XML comments, text nodes, processing instructions, and more. This functionality is important, for example, to someone who is writing a word processor and wants to save documents as XML. However, the majority of XML programmers are primarily concerned with elements, attributes, and their values.
Methods for Retrieving a Collection of Elements
Method for Retrieving a Single Element
The following method retrieves a single child from an XElement object.
Method for Retrieving a Collection of Attributes
The following method retrieves attributes from an XElement object.
Method for Retrieving a Single Attribute
The following method retrieves a single attribute from an XElement object.
ConceptsLINQ to XML Axes