Extensions.Descendants メソッド

定義

ソース コレクション内のすべての要素とドキュメントの子孫要素を含む要素のコレクションを返します。

オーバーロード

名前 説明
Descendants<T>(IEnumerable<T>, XName)

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

Descendants<T>(IEnumerable<T>)

ソース コレクション内のすべての要素とドキュメントの子孫要素を含む要素のコレクションを返します。

注釈

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

このメソッドでは、遅延実行が使用されます。

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

ソース:
Extensions.cs
ソース:
Extensions.cs
ソース:
Extensions.cs
ソース:
Extensions.cs
ソース:
Extensions.cs

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

public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Collections::Generic::IEnumerable<T> ^ source, System::Xml::Linq::XName ^ name);
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;
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;
static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T), name As XName) As IEnumerable(Of XElement)

型パラメーター

T

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

パラメーター

source
IEnumerable<T>

ソース コレクションを含むIEnumerable<T>XContainer

name
XName

照合する XName

返品

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

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

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);
Dim xmlTree As XElement = _
    <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>

Dim str As String = _
    ( _
        From el In xmlTree.<Para>...<t> _
        Select CStr(el) _
    ) _
    .Aggregate(New StringBuilder(), _
               Function(ByVal sb, ByVal i) sb.Append(i), _
               Function(ByVal 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 名前空間の操作」を参照してください。

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);
Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = _
            <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>

        Dim str As String = _
            ( _
                From el In xmlTree.<Para>...<t> _
                Select CStr(el) _
            ) _
            .Aggregate(New StringBuilder(), _
                       Function(sb, i) sb.Append(i), _
                       Function(sb) sb.ToString())

        Console.WriteLine(str)
    End Sub
End Module

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

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) のLanguage-Integrated軸を使用できます。

このメソッドでは、遅延実行が使用されます。

こちらもご覧ください

適用対象

Descendants<T>(IEnumerable<T>)

ソース:
Extensions.cs
ソース:
Extensions.cs
ソース:
Extensions.cs
ソース:
Extensions.cs
ソース:
Extensions.cs

ソース コレクション内のすべての要素とドキュメントの子孫要素を含む要素のコレクションを返します。

public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Collections::Generic::IEnumerable<T> ^ source);
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;
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;
static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XElement)

型パラメーター

T

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

パラメーター

source
IEnumerable<T>

ソース コレクションを含むIEnumerable<T>XContainer

返品

ソース コレクション内のすべての要素とドキュメントの子孫要素を含むIEnumerable<T>XElement

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

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);
Dim xmlTree As XElement = _
    <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>

Dim elList = From el In xmlTree.<Para>.Descendants _
                        Select el

For Each el As XElement In elList
    Console.WriteLine(el)
Next

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

<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 名前空間の操作」を参照してください。

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);
Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = _
            <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>

        Dim elList = From el In xmlTree.<Para>.Descendants _
                                Select el

        For Each el As XElement In elList
            Console.WriteLine(el)
        Next
    End Sub
End Module

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

<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すべての子孫を取得する場合は、この軸メソッドを明示的に使用する必要があります。

このメソッドでは、遅延実行が使用されます。

こちらもご覧ください

適用対象