Extensions.Descendants Método

Definição

Retorna uma coleção de elementos que contém os elementos descendentes de cada elemento e documento na coleção de origem.

Sobrecargas

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

Retorna uma coleção filtrada de elementos que contém os elementos descendentes 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.

Descendants<T>(IEnumerable<T>)

Retorna uma coleção de elementos que contém os elementos descendentes de cada elemento e documento na coleção de origem.

Comentários

Visual Basic usuários podem usar o eixo descendente XML integrado para recuperar os elementos descendentes de uma coleção. No entanto, o eixo integrado recupera apenas descendentes com um nome especificado. Se Visual Basic usuários quiserem recuperar todos os descendentes, eles deverão usar esse método de eixo explicitamente.

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

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

Origem:
Extensions.cs
Origem:
Extensions.cs
Origem:
Extensions.cs
Origem:
Extensions.cs
Origem:
Extensions.cs

Retorna uma coleção filtrada de elementos que contém os elementos descendentes 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 ^> ^ 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)

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 XContainer origem.

name
XName

A XName correspondência a ser correspondida.

Retornos

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

Exemplos

O exemplo a seguir recupera uma coleção de dois elementos e recupera uma coleção de todos os descendentes dos dois elementos que têm o nome do elemento especificado.

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)

Este exemplo produz a seguinte saída:

This is some text where all of the text nodes must be concatenated. This is a second sentence.

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

Este exemplo produz a seguinte saída:

This is some text where all of the text nodes must be concatenated. This is a second sentence.

Comentários

Visual Basic usuários podem usar os eixos Language-Integrated em Visual Basic (LINQ to XML) em vez de usar esse método de eixo explicitamente.

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

Confira também

Aplica-se a

Descendants<T>(IEnumerable<T>)

Origem:
Extensions.cs
Origem:
Extensions.cs
Origem:
Extensions.cs
Origem:
Extensions.cs
Origem:
Extensions.cs

Retorna uma coleção de elementos que contém os elementos descendentes 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 ^> ^ 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)

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 XContainer origem.

Retornos

Um IEnumerable<T> deles contém os elementos descendentes de cada elemento e documento na coleção de XElement origem.

Exemplos

O exemplo a seguir recupera uma coleção de elementos e usa esse método de eixo para recuperar todos os elementos descendentes de cada item na coleção de elementos.

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

Este exemplo produz a seguinte saída:

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

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

Este exemplo produz a seguinte saída:

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

Comentários

Visual Basic usuários podem usar o eixo descendente XML integrado para recuperar os elementos descendentes de uma coleção. No entanto, o eixo integrado recupera apenas descendentes com um nome especificado. Se Visual Basic usuários quiserem recuperar todos os descendentes, eles deverão usar esse método de eixo explicitamente.

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

Confira também

Aplica-se a