XContainer.DescendantNodes Metodo

Definizione

Restituisce una raccolta di nodi discendenti del documento o elemento nell'ordine dei documenti.

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

Restituisce

IEnumerable<XNode>

IEnumerable<T> di XNode che contiene i nodi discendenti del XContainer nell'ordine dei documenti.

Esempio

L'esempio seguente crea un albero XML e quindi esegue l'iterazione tramite l'asse DescendantNodes .

XElement xmlTree = new XElement("Root",  
    // Attributes are not nodes, so will not be returned by DescendantNodes.  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XNode> dnas =  
    from node in xmlTree.DescendantNodes()  
    select node;  
foreach (XNode node in dnas)  
{  
    if (node is XElement)  
        Console.WriteLine((node as XElement).Name);  
    else  
        Console.WriteLine(node);  
}  
' Attributes are not nodes, so will not be returned by DescendantNodes.  
Dim xmlTree As XElement = _   
    <Root Att1="AttributeContent">  
        <Child>  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  

Dim dnas = From node In xmlTree.DescendantNodes _  
           Select node  

For Each node In dnas  
    If TypeOf node Is XElement Then  
        Console.WriteLine(DirectCast(node, XElement).Name)  
    Else  
        Console.WriteLine(node)  
    End If  
Next  

Nell'esempio viene prodotto l'output seguente:

Child  
GrandChild  
element content  

Commenti

Si noti che gli attributi non vengono considerati nodi in LINQ to XML, quindi non faranno parte della raccolta restituita da questo metodo.

Questo metodo usa l'esecuzione posticipata.

Si applica a

Vedi anche