Extensions.Elements Método

Definição

Retorna uma coleção dos elementos filho de cada elemento e documento na coleção de origem.

Sobrecargas

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

Retorna uma coleção dos elementos filho de cada elemento e documento na coleção de origem.

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

Retorna uma coleção filtrada dos elementos filho de cada elemento e documento na coleção de origem. Somente os elementos que têm uma XName correspondente são incluídos na coleção.

Comentários

Visual Basic contém um eixo de elementos integrados que permite localizar todos os elementos filho com um XName especificado para cada elemento na coleção de origem.

Esse método usa a execução adiada.

Elements<T>(IEnumerable<T>)

Retorna uma coleção dos elementos filho de cada elemento e documento na coleção de origem.

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

Parâmetros de tipo

T

O tipo dos objetos em source, restrito a XContainer.

Parâmetros

source
IEnumerable<T>

Um IEnumerable<T> deles contém a coleção de XElement origem.

Retornos

Um IEnumerable<T> dos XElement elementos filho de cada elemento ou documento na coleção de origem.

Exemplos

O exemplo a seguir recupera uma coleção de elementos com o nome do elemento .Child Em seguida, ele usa esse método de eixo para recuperar todos os elementos filho da coleção.

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

Este exemplo produz a seguinte saída:

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

O exemplo a seguir é o mesmo, mas nesse caso o XML está em um namespace. Para obter mais informações, consulte Trabalhar com namespaces 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

Este exemplo produz a seguinte saída:

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

Comentários

Embora Visual Basic contenha um eixo de elementos integrados que permite localizar todos os elementos filho com um XName especificado para cada elemento da coleção de origem, não há nenhum eixo de elementos integrados que permita recuperar uma coleção de cada elemento filho para cada elemento na coleção de origem.

Esse método usa a execução adiada.

Confira também

Aplica-se a

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

Retorna uma coleção filtrada dos elementos filho de cada elemento e documento na coleção de origem. Somente os elementos que têm uma XName correspondente são incluídos na coleção.

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

Parâmetros de tipo

T

O tipo dos objetos em source, restrito a XContainer.

Parâmetros

source
IEnumerable<T>

Um IEnumerable<T> deles contém a coleção de XElement origem.

name
XName

A XName correspondência a ser correspondida.

Retornos

Um IEnumerable<T> dos XElement elementos filho de cada elemento e documento na coleção de origem. Somente os elementos que têm uma XName correspondente são incluídos na coleção.

Exemplos

Esse método de extensão é útil quando você deseja recuperar todos os elementos com um nome especificado em uma profundidade específica. Isso é fácil se o documento for muito regular, mas se o documento for irregular, poderá ser um pouco mais difícil. No exemplo a seguir, queremos recuperar todos os aaa elementos filhos de Item elementos. Um determinado Item elemento pode ou não conter um aaa elemento. Isso é feito facilmente usando esse método de extensão, da seguinte maneira:

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

Este exemplo produz a seguinte saída:

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

O exemplo a seguir é o mesmo, mas nesse caso o XML está em um namespace. Para obter mais informações, consulte Trabalhar com namespaces 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

Este exemplo produz a seguinte saída:

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

Comentários

Visual Basic usuários podem usar o eixo de elementos integrados para recuperar os elementos filho de cada elemento em uma coleção.

Esse método usa a execução adiada.

Confira também

Aplica-se a