英語で読む

次の方法で共有


Extensions.Descendants メソッド

定義

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納された要素のコレクションを返します。

オーバーロード

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

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納され、フィルター処理された要素のコレクションを返します。 一致する XName を持つ要素のみがコレクションに含められます。

Descendants<T>(IEnumerable<T>)

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納された要素のコレクションを返します。

注釈

Visual Basicユーザーは、統合 XML 子孫軸を使用して、コレクションの子孫要素を取得できます。 ただし、統合軸は、指定された名前の子孫のみを取得します。 ユーザー Visual Basicすべての子孫を取得する場合は、この軸メソッドを明示的に使用する必要があります。

このメソッドは遅延実行を使用します。

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

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納され、フィルター処理された要素のコレクションを返します。 一致する XName を持つ要素のみがコレクションに含められます。

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;

型パラメーター

T

XContainer に制限された、source 内のオブジェクトの型。

パラメーター

source
IEnumerable<T>

ソース コレクションが格納されている IEnumerable<T>XContainer

name
XName

照合する XName

戻り値

IEnumerable<XElement>

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納された、IEnumerable<T>XElement。 一致する XName を持つ要素のみがコレクションに含められます。

次の例では、2 つの要素のコレクションを取得し、指定した要素名を持つ 2 つの要素のすべての子孫のコレクションを取得します。

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 is some text where all of the text nodes must be concatenated. This is a second sentence.  

同じ例を次に示しますが、この場合、XML は名前空間にあります。 詳細については、「 XML 名前空間の操作」を参照してください。

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 is some text where all of the text nodes must be concatenated. This is a second sentence.  

注釈

Visual Basicユーザーは、この軸メソッドを明示的に使用する代わりに、Visual Basic (LINQ to XML) で言語統合軸を使用できます。

このメソッドは遅延実行を使用します。

こちらもご覧ください

適用対象

.NET 7 およびその他のバージョン
製品 バージョン
.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
.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
.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>)

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納された要素のコレクションを返します。

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;

型パラメーター

T

XContainer に制限された、source 内のオブジェクトの型。

パラメーター

source
IEnumerable<T>

ソース コレクションが格納されている IEnumerable<T>XContainer

戻り値

IEnumerable<XElement>

ソース コレクション内のすべての要素とドキュメントの子孫要素が格納された、IEnumerable<T>XElement

次の例では、要素のコレクションを取得し、この軸メソッドを使用して、要素のコレクション内のすべての項目のすべての子孫要素を取得します。

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);  

この例を実行すると、次の出力が生成されます。

<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>  

同じ例を次に示しますが、この場合、XML は名前空間にあります。 詳細については、「 XML 名前空間の操作」を参照してください。

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);  

この例を実行すると、次の出力が生成されます。

<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>  

注釈

Visual Basicユーザーは、統合 XML 子孫軸を使用して、コレクションの子孫要素を取得できます。 ただし、統合軸は、指定された名前の子孫のみを取得します。 ユーザー Visual Basicすべての子孫を取得する場合は、この軸メソッドを明示的に使用する必要があります。

このメソッドは遅延実行を使用します。

こちらもご覧ください

適用対象

.NET 7 およびその他のバージョン
製品 バージョン
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0