XContainer.Descendants Method

Definition

Returns a collection of the descendant elements for this document or element, in document order.

Overloads

Descendants()

Returns a collection of the descendant elements for this document or element, in document order.

Descendants(XName)

Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection.

Remarks

This method uses deferred execution.

Descendants()

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Returns a collection of the descendant elements for this document or element, in document order.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants();

Returns

An IEnumerable<T> of XElement containing the descendant elements of the XContainer.

Examples

The following example creates an XML tree, and then uses this axis method to retrieve the descendants.

C#
XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> de =  
    from el in xmlTree.Descendants()  
    select el;  
foreach (XElement el in de)  
    Console.WriteLine(el.Name);  

This example produces the following output:

Child  
GrandChild  

Remarks

Note that this method will not return itself in the resulting IEnumerable<T>. See DescendantsAndSelf if you need to include the current XElement in the results.

This method uses deferred execution.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Descendants(XName)

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName? name);

Parameters

name
XName

The XName to match.

Returns

An IEnumerable<T> of XElement containing the descendant elements of the XContainer that match the specified XName.

Examples

The following example prints all descendants of an element.

C#
// Attributes are not nodes, so will not be returned by DescendantNodes.  
XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> de =  
    from el in xmlTree.Descendants("Child")  
    select el;  
foreach (XElement el in de)  
    Console.WriteLine(el.Name);  

This example produces the following output:

Child  

The following is the same example, but in this case the XML is in a namespace. For more information, see Work with XML Namespaces.

C#
// Attributes are not nodes, so will not be returned by DescendantNodes.  
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(aw + "Att1", "AttributeContent"),  
    new XElement(aw + "Child",  
        new XText("Some text"),  
        new XElement(aw + "GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> de =  
    from el in xmlTree.Descendants(aw + "Child")  
    select el;  
foreach (XElement el in de)  
    Console.WriteLine(el.Name);  

This example produces the following output:

{http://www.adventure-works.com}Child  

Remarks

This method uses deferred execution.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0