Extensions.Descendants Method

Definition

Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Overloads

Descendants<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

Descendants<T>(IEnumerable<T>)

Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Remarks

Visual Basic users can use the integrated XML descendant axis to retrieve the descendant elements of a collection. However, the integrated axis only retrieves descendants with a specified name. If Visual Basic users want to retrieve all descendants, then they must use this axis method explicitly.

This method uses deferred execution.

Descendants<T>(IEnumerable<T>, XName)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T> source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XContainer;
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T?> source, System.Xml.Linq.XName? name) where T : System.Xml.Linq.XContainer;

Type Parameters

T

The type of the objects in source, constrained to XContainer.

Parameters

source
IEnumerable<T>

An IEnumerable<T> of XContainer that contains the source collection.

name
XName

The XName to match.

Returns

An IEnumerable<T> of XElement that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

Examples

The following example retrieves a collection of two elements, and then retrieves a collection of all descendants of the two elements that have the specified element name.

C#
XElement xmlTree = XElement.Parse(  
@"<Root>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the text nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  

string str =  
    (from el in xmlTree.Elements("Para").Descendants("t")  
    select (string)el)  
    .Aggregate(new StringBuilder(),  
        (sb, i) => sb.Append(i),  
        sb => sb.ToString());  

Console.WriteLine(str);  

This example produces the following output:

This is some text where all of the text nodes must be concatenated. This is a second sentence.  

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#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = XElement.Parse(  
@"<Root xmlns='http://www.adventure-works.com'>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the text nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  

string str =  
    (from el in xmlTree.Elements(aw + "Para").Descendants(aw + "t")  
     select (string)el)  
    .Aggregate(new StringBuilder(),  
        (sb, i) => sb.Append(i),  
        sb => sb.ToString());  

Console.WriteLine(str);  

This example produces the following output:

This is some text where all of the text nodes must be concatenated. This is a second sentence.  

Remarks

Visual Basic users can use the Language-Integrated Axes in Visual Basic (LINQ to XML) instead of using this axis method explicitly.

This method uses deferred execution.

See also

Applies to

.NET 9 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
.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<T>(IEnumerable<T>)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;

Type Parameters

T

The type of the objects in source, constrained to XContainer.

Parameters

source
IEnumerable<T>

An IEnumerable<T> of XContainer that contains the source collection.

Returns

An IEnumerable<T> of XElement that contains the descendant elements of every element and document in the source collection.

Examples

The following example retrieves a collection of elements, and then uses this axis method to retrieve all descendent elements of every item in the collection of elements.

C#
XElement xmlTree = XElement.Parse(  
@"<Root>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  

IEnumerable<XElement> elList =  
    from el in xmlTree.Elements("Para").Descendants()  
    select el;  

foreach (XElement el in elList)  
    Console.WriteLine(el);  

This example produces the following output:

<t>This is some text </t>  
<b>  
  <t>where</t>  
</b>  
<t>where</t>  
<t> all of the nodes must be concatenated. </t>  
<t>This is a second sentence.</t>  

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#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = XElement.Parse(  
@"<Root xmlns='http://www.adventure-works.com'>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  

IEnumerable<XElement> elList =  
    from el in xmlTree.Elements(aw + "Para").Descendants()  
    select el;  

foreach (XElement el in elList)  
    Console.WriteLine(el);  

This example produces the following output:

<t xmlns="http://www.adventure-works.com">This is some text </t>  
<b xmlns="http://www.adventure-works.com">  
  <t>where</t>  
</b>  
<t xmlns="http://www.adventure-works.com">where</t>  
<t xmlns="http://www.adventure-works.com"> all of the nodes must be concatenated. </t>  
<t xmlns="http://www.adventure-works.com">This is a second sentence.</t>  

Remarks

Visual Basic users can use the integrated XML descendant axis to retrieve the descendant elements of a collection. However, the integrated axis only retrieves descendants with a specified name. If Visual Basic users want to retrieve all descendants, then they must use this axis method explicitly.

This method uses deferred execution.

See also

Applies to

.NET 9 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
.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