Extensions.Elements 메서드

정의

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

오버로드

Name Description
Elements<T>(IEnumerable<T>)

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

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

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

설명

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

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

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

에 제한되는 source개체XContainer의 형식입니다.

매개 변수

source
IEnumerable<T>

IEnumerable<T> 원본 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)

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

에 제한되는 source개체XContainer의 형식입니다.

매개 변수

source
IEnumerable<T>

IEnumerable<T> 원본 XElement 컬렉션이 포함된 항목입니다.

name
XName

XName 일치할 항목입니다.

반품

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

예제

이 확장 메서드는 특정 깊이에서 지정된 이름의 모든 요소를 검색하려는 경우에 유용합니다. 문서가 매우 일반적인 경우에는 쉽지만 문서가 불규칙한 경우에는 좀 더 어려울 수 있습니다. 다음 예제에서는 요소의 aaa 자식인 모든 Item 요소를 검색하려고 합니다. 지정된 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 사용자는 통합 요소 축을 사용하여 컬렉션에 있는 모든 요소의 자식 요소를 검색할 수 있습니다.

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

추가 정보

적용 대상