Extensions.Elements 메서드

정의

소스 컬렉션에 있는 모든 요소 및 문서의 자식 요소 컬렉션을 반환합니다.

오버로드

Elements<T>(IEnumerable<T>)

소스 컬렉션에 있는 모든 요소 및 문서의 자식 요소 컬렉션을 반환합니다.

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

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

설명

Visual Basic 소스 컬렉션의 모든 요소에 대해 지정된 XName 모든 자식 요소를 찾을 수 있는 통합 요소 축을 포함합니다.

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

Elements<T>(IEnumerable<T>)

소스 컬렉션에 있는 모든 요소 및 문서의 자식 요소 컬렉션을 반환합니다.

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

형식 매개 변수

T

XContainer로 제한된 source의 개체 형식입니다.

매개 변수

source
IEnumerable<T>

소스 컬렉션이 들어 있는 IEnumerable<T>XElement입니다.

반환

IEnumerable<XElement>

소스 컬렉션에 있는 모든 요소 또는 문서의 자식 요소에 대한 IEnumerable<T>XElement입니다.

예제

다음 예제에서는 요소 이름의 Child요소 컬렉션을 검색합니다. 그런 다음 이 축 메서드를 사용하여 컬렉션의 모든 자식 요소를 검색합니다.

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild1", 1),  
        new XElement("GrandChild2", 2)  
    ),  
    new XElement("Child",  
        new XElement("GrandChild3", 3),  
        new XElement("GrandChild4", 4)  
    ),  
    new XElement("Child",  
        new XElement("GrandChild5", 5),  
        new XElement("GrandChild6", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements("Child").Elements()  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
     <Root>  
          <Child>  
              <GrandChild1>1</GrandChild1>  
              <GrandChild2>2</GrandChild2>  
          </Child>  

          <Child>  
              <GrandChild3>3</GrandChild3>  
              <GrandChild4>4</GrandChild4>  
          </Child>  

          <Child>  
              <GrandChild5>5</GrandChild5>  
              <GrandChild6>6</GrandChild6>  
          </Child>  
      </Root>  

Dim allGrandChildren = From el In xmlTree.<Child>.Elements _  
                       Select el  

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

이 예제는 다음과 같은 출력을 생성합니다.

<GrandChild1>1</GrandChild1>  
<GrandChild2>2</GrandChild2>  
<GrandChild3>3</GrandChild3>  
<GrandChild4>4</GrandChild4>  
<GrandChild5>5</GrandChild5>  
<GrandChild6>6</GrandChild6>  

다음은 동일한 예제이지만 이 경우 XML은 네임스페이스에 있습니다. 자세한 내용은 XML 네임스페이스 작업을 참조하세요.

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild1", 1),  
        new XElement(aw + "GrandChild2", 2)  
    ),  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild3", 3),  
        new XElement(aw + "GrandChild4", 4)  
    ),  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild5", 5),  
        new XElement(aw + "GrandChild6", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements(aw + "Child").Elements()  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
             <Root>  
                 <Child>  
                     <GrandChild1>1</GrandChild1>  
                     <GrandChild2>2</GrandChild2>  
                 </Child>  

                 <Child>  
                     <GrandChild3>3</GrandChild3>  
                     <GrandChild4>4</GrandChild4>  
                 </Child>  

                 <Child>  
                     <GrandChild5>5</GrandChild5>  
                     <GrandChild6>6</GrandChild6>  
                 </Child>  
             </Root>  

        Dim allGrandChildren = From el In xmlTree.<Child>.Elements _  
                               Select el  

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

이 예제는 다음과 같은 출력을 생성합니다.

<GrandChild1 xmlns="http://www.adventure-works.com">1</GrandChild1>  
<GrandChild2 xmlns="http://www.adventure-works.com">2</GrandChild2>  
<GrandChild3 xmlns="http://www.adventure-works.com">3</GrandChild3>  
<GrandChild4 xmlns="http://www.adventure-works.com">4</GrandChild4>  
<GrandChild5 xmlns="http://www.adventure-works.com">5</GrandChild5>  
<GrandChild6 xmlns="http://www.adventure-works.com">6</GrandChild6>  

설명

Visual Basic 소스 컬렉션의 모든 요소에 대해 지정된 XName 모든 자식 요소를 찾을 수 있는 통합 요소 축이 포함되어 있지만 소스 컬렉션의 모든 요소에 대해 모든 자식 요소의 컬렉션을 검색할 수 있는 통합 요소 축은 없습니다.

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

추가 정보

적용 대상

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

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

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

형식 매개 변수

T

XContainer로 제한된 source의 개체 형식입니다.

매개 변수

source
IEnumerable<T>

소스 컬렉션이 들어 있는 IEnumerable<T>XElement입니다.

name
XName

일치시킬 XName입니다.

반환

IEnumerable<XElement>

소스 컬렉션에 있는 모든 요소 및 문서의 자식 요소에 대한 IEnumerable<T>XElement입니다. 일치하는 XName이 있는 요소만 컬렉션에 포함됩니다.

예제

이 확장 메서드는 특정 깊이에서 지정된 이름의 모든 요소를 검색하려는 경우에 유용합니다. 문서가 매우 일반적이면 쉽지만 문서가 불규칙한 경우 좀 더 어려울 수 있습니다. 다음 예제에서는 요소의 Item 자식인 모든 aaa 요소를 검색하려고 합니다. 지정된 Item 요소에 요소가 포함될 수도 있거나 포함되지 aaa 않을 수도 있습니다. 이 작업은 다음과 같이 이 확장 메서드를 사용하여 쉽게 수행할 수 있습니다.

XElement xmlTree = new XElement("Root",  
    new XElement("Item",  
        new XElement("aaa", 1),  
        new XElement("bbb", 2)  
    ),  
    new XElement("Item",  
        new XElement("ccc", 3),  
        new XElement("aaa", 4)  
    ),  
    new XElement("Item",  
        new XElement("ddd", 5),  
        new XElement("eee", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements("Item").Elements("aaa")  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
    <Root>  
        <Item>  
            <aaa>1</aaa>  
            <bbb>2</bbb>  
        </Item>  

        <Item>  
            <ccc>3</ccc>  
            <aaa>4</aaa>  
        </Item>  

        <Item>  
            <ddd>5</ddd>  
            <eee>6</eee>  
        </Item>  
    </Root>  

Dim allGrandChildren = From el In xmlTree.<Item>.<aaa> _  
                       Select el  

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

이 예제는 다음과 같은 출력을 생성합니다.

<aaa>1</aaa>  
<aaa>4</aaa>  

다음은 동일한 예제이지만 이 경우 XML은 네임스페이스에 있습니다. 자세한 내용은 XML 네임스페이스 작업을 참조하세요.

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XElement(aw + "Item",  
        new XElement(aw + "aaa", 1),  
        new XElement(aw + "bbb", 2)  
    ),  
    new XElement(aw + "Item",  
        new XElement(aw + "ccc", 3),  
        new XElement(aw + "aaa", 4)  
    ),  
    new XElement(aw + "Item",  
        new XElement(aw + "ddd", 5),  
        new XElement(aw + "eee", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements(aw + "Item").Elements(aw + "aaa")  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <Root>  
                <Item>  
                    <aaa>1</aaa>  
                    <bbb>2</bbb>  
                </Item>  

                <Item>  
                    <ccc>3</ccc>  
                    <aaa>4</aaa>  
                </Item>  

                <Item>  
                    <ddd>5</ddd>  
                    <eee>6</eee>  
                </Item>  
            </Root>  

        Dim allGrandChildren = From el In xmlTree.<Item>.<aaa> _  
                               Select el  

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

이 예제는 다음과 같은 출력을 생성합니다.

<aaa xmlns="http://www.adventure-works.com">1</aaa>  
<aaa xmlns="http://www.adventure-works.com">4</aaa>  

설명

Visual Basic 사용자는 통합 요소 축을 사용하여 컬렉션에 있는 모든 요소의 자식 요소를 검색할 수 있습니다.

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

추가 정보

적용 대상