XContainer.Descendants Método

Definição

Retorna uma coleção dos elementos descendentes deste documento ou elemento, na ordem do documento.

Sobrecargas

Nome Description
Descendants()

Retorna uma coleção dos elementos descendentes deste documento ou elemento, na ordem do documento.

Descendants(XName)

Retorna uma coleção filtrada dos elementos descendentes deste documento ou elemento, na ordem do documento. Somente os elementos que têm uma XName correspondente são incluídos na coleção.

Comentários

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

Descendants()

Origem:
XContainer.cs
Origem:
XContainer.cs
Origem:
XContainer.cs
Origem:
XContainer.cs
Origem:
XContainer.cs

Retorna uma coleção dos elementos descendentes deste documento ou elemento, na ordem do documento.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants();
member this.Descendants : unit -> seq<System.Xml.Linq.XElement>
Public Function Descendants () As IEnumerable(Of XElement)

Retornos

Um IEnumerable<T> dos XElement elementos descendentes do XContainer.

Exemplos

O exemplo a seguir cria uma árvore XML e usa esse método de eixo para recuperar os descendentes.

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XElement> de =
    from el in xmlTree.Descendants()
    select el;
foreach (XElement el in de)
    Console.WriteLine(el.Name);
' Attributes are not nodes, so will not be returned by DescendantNodes.
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
        <Child>Some text
            <GrandChild>element content</GrandChild>
        </Child>
    </Root>
Dim de = From el In xmlTree.Descendants _
         Select el

For Each el In de
    Console.WriteLine(el.Name)
Next

Este exemplo produz a seguinte saída:

Child
GrandChild

Comentários

Observe que esse método não retornará a si mesmo no resultado IEnumerable<T>. Veja DescendantsAndSelf se você precisa incluir a corrente XElement nos resultados.

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

Confira também

Aplica-se a

Descendants(XName)

Origem:
XContainer.cs
Origem:
XContainer.cs
Origem:
XContainer.cs
Origem:
XContainer.cs
Origem:
XContainer.cs

Retorna uma coleção filtrada dos elementos descendentes deste documento ou elemento, na ordem do documento. Somente os elementos que têm uma XName correspondente são incluídos na coleção.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName? name);
member this.Descendants : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Descendants (name As XName) As IEnumerable(Of XElement)

Parâmetros

name
XName

A XName correspondência a ser correspondida.

Retornos

Um IEnumerable<T> dos XElement elementos descendentes que XContainer correspondem ao especificado XName.

Exemplos

O exemplo a seguir imprime todos os descendentes de um elemento.

// Attributes are not nodes, so will not be returned by DescendantNodes.
XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XElement> de =
    from el in xmlTree.Descendants("Child")
    select el;
foreach (XElement el in de)
    Console.WriteLine(el.Name);
' Attributes are not nodes, so will not be returned by the descendants axis.
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
         <Child>Some text
             <GrandChild>element content</GrandChild>
         </Child>
     </Root>

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

For Each el In de
    Console.WriteLine(el.Name)
Next

Este exemplo produz a seguinte saída:

Child

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.

// Attributes are not nodes, so will not be returned by DescendantNodes.
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
    new XAttribute(aw + "Att1", "AttributeContent"),
    new XElement(aw + "Child",
        new XText("Some text"),
        new XElement(aw + "GrandChild", "element content")
    )
);
IEnumerable<XElement> de =
    from el in xmlTree.Descendants(aw + "Child")
    select el;
foreach (XElement el in de)
    Console.WriteLine(el.Name);
Imports <xmlns:aw = "http://www.adventure-works.com">

Module Module1
    Sub Main()
        ' Attributes are not nodes, so will not be returned by the descendants axis.
        Dim xmlTree As XElement = _
            <aw:Root aw:Att1="AttributeContent">
                 <aw:Child>Some text
                     <aw:GrandChild>element content</aw:GrandChild>
                 </aw:Child>
             </aw:Root>

        Dim de = From el In xmlTree...<aw:Child> _
                 Select el

        For Each el In de
            Console.WriteLine(el.Name)
        Next
    End Sub
End Module

Este exemplo produz a seguinte saída:

{http://www.adventure-works.com}Child

Comentários

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

Confira também

Aplica-se a