Extensions.DescendantNodes<T>(IEnumerable<T>) Método

Definição

Retorna uma coleção de nós descendentes de cada documento e elemento 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::XNode ^> ^ DescendantNodes(System::Collections::Generic::IEnumerable<T> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> DescendantNodes<T>(this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> DescendantNodes<T>(this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;
static member DescendantNodes : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XNode> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function DescendantNodes(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XNode)

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> dos XNode nós descendentes de cada documento e elemento na coleção de origem.

Exemplos

O exemplo a seguir recupera uma coleção de dois elementos e recupera uma coleção de todos os nós descendentes para cada elemento na coleção de origem. Observe que o atributo do GrandChild elemento não é exibido como um nó.

XElement xmlTree = XElement.Parse(
@"<Root>
    <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>
        <!--a comment-->
        <?xml-stylesheet type='text/xsl' href='test.xsl'?>
    </Child>
    <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>
</Root>");
IEnumerable<XNode> nodes =
    from node in xmlTree.Elements("Child").DescendantNodes()
    select node;

foreach (XNode node in nodes)
{
    switch (node.NodeType)
    {
        case XmlNodeType.Element:
            Console.WriteLine("Element: {0}", ((XElement)node).Name);
            break;
        case XmlNodeType.Text:
            Console.WriteLine("Text: {0}", ((XText)node).Value);
            break;
        case XmlNodeType.Comment:
            Console.WriteLine("Comment: {0}", ((XComment)node).Value);
            break;
        case XmlNodeType.ProcessingInstruction:
            Console.WriteLine("PI: {0}", ((XProcessingInstruction)node).Data);
            break;
    }
}
Dim xmlTree As XElement = _
<Root>
    <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>
        <!--a comment-->
        <?xml-stylesheet type='text/xsl' href='test.xsl'?>
    </Child>
    <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>
</Root>

Dim nodes As IEnumerable(Of XNode) = _
    From node In xmlTree.<Child>.DescendantNodes _
    Select node

For Each node As XNode In nodes
    Select Case node.NodeType
        Case XmlNodeType.Element
            Console.WriteLine("Element: {0}", DirectCast(node, XElement).Name)
        Case XmlNodeType.Text
            Console.WriteLine("Text: {0}", DirectCast(node, XText).Value)
        Case XmlNodeType.Comment
            Console.WriteLine("Comment: {0}", DirectCast(node, XComment).Value)
        Case XmlNodeType.ProcessingInstruction
            Console.WriteLine("PI: {0}", DirectCast(node, XProcessingInstruction).Data)
    End Select
Next

Este exemplo produz a seguinte saída:

Text: aaa
Element: GrandChild
Text: Text
Comment: a comment
PI: type='text/xsl' href='test.xsl'
Text: ccc
Element: GrandChild
Text: Text
Text: ddd

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'>
    <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>
        <!--a comment-->
        <?xml-stylesheet type='text/xsl' href='test.xsl'?>
    </Child>
    <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>
</Root>");
IEnumerable<XNode> nodes =
    from node in xmlTree.Elements(aw + "Child").DescendantNodes()
    select node;

foreach (XNode node in nodes)
{
    switch (node.NodeType)
    {
        case XmlNodeType.Element:
            Console.WriteLine("Element: {0}", ((XElement)node).Name);
            break;
        case XmlNodeType.Text:
            Console.WriteLine("Text: {0}", ((XText)node).Value);
            break;
        case XmlNodeType.Comment:
            Console.WriteLine("Comment: {0}", ((XComment)node).Value);
            break;
        case XmlNodeType.ProcessingInstruction:
            Console.WriteLine("PI: {0}", ((XProcessingInstruction)node).Data);
            break;
    }
}
Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = _
        <Root>
            <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>
                <!--a comment-->
                <?xml-stylesheet type='text/xsl' href='test.xsl'?>
            </Child>
            <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>
        </Root>

        Dim nodes As IEnumerable(Of XNode) = _
            From node In xmlTree.<Child>.DescendantNodes _
            Select node

        For Each node As XNode In nodes
            Select Case node.NodeType
                Case XmlNodeType.Element
                    Console.WriteLine("Element: {0}", DirectCast(node, XElement).Name)
                Case XmlNodeType.Text
                    Console.WriteLine("Text: {0}", DirectCast(node, XText).Value)
                Case XmlNodeType.Comment
                    Console.WriteLine("Comment: {0}", DirectCast(node, XComment).Value)
                Case XmlNodeType.ProcessingInstruction
                    Console.WriteLine("PI: {0}", DirectCast(node, XProcessingInstruction).Data)
            End Select
        Next
    End Sub
End Module

Este exemplo produz a seguinte saída:

Text: aaa
Element: {http://www.adventure-works.com}GrandChild
Text: Text
Comment: a comment
PI: type='text/xsl' href='test.xsl'
Text: ccc
Element: {http://www.adventure-works.com}GrandChild
Text: Text
Text: ddd

Comentários

Esse método de extensão de eixo é usado e XDocumentXElement objetos. Esses dois tipos derivam de XContainer, portanto, esse método opera em um IEnumerable<T> dos quais contém a coleção de XContainer origem.

Embora Visual Basic tenha um eixo XML integrado para elementos descendentes, não há eixo integrado para nós descendentes, portanto, Visual Basic usuários devem usar esse método de eixo explicitamente.

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

Aplica-se a

Confira também