Extensions.Descendants 메서드

정의

소스 컬렉션에 있는 모든 요소와 문서의 하위 요소를 포함하는 요소의 컬렉션을 반환합니다.

오버로드

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

소스 컬렉션에 있는 모든 요소와 문서의 하위 요소를 포함하는 필터링된 요소 컬렉션을 반환합니다. 일치하는 XName 있는 요소만 컬렉션에 포함됩니다.

Descendants<T>(IEnumerable<T>)

소스 컬렉션에 있는 모든 요소와 문서의 하위 요소를 포함하는 요소의 컬렉션을 반환합니다.

설명

Visual Basic 사용자는 통합 XML 하위 축을 사용하여 컬렉션의 하위 요소를 검색할 수 있습니다. 그러나 통합 축은 지정된 이름의 하위 항목만 검색합니다. Visual Basic 사용자가 모든 하위 항목을 검색하려는 경우 이 축 메서드를 명시적으로 사용해야 합니다.

이 메서드는 지연된 실행을 사용합니다.

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

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs
Source:
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 있는 요소만 컬렉션에 포함됩니다.

예제

다음 예제에서는 두 요소의 컬렉션을 검색한 다음 지정된 요소 이름을 가진 두 요소의 모든 하위 항목 컬렉션을 검색합니다.

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

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs
Source:
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 사용자가 모든 하위 항목을 검색하려는 경우 이 축 메서드를 명시적으로 사용해야 합니다.

이 메서드는 지연된 실행을 사용합니다.

추가 정보

적용 대상