Extensions.DescendantNodes<T>(IEnumerable<T>) Metodo

Definizione

Restituisce una raccolta di nodi discendenti di ciascun documento ed elemento nella raccolta di origine.

C#
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;
C#
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;

Parametri di tipo

T

Tipo di oggetti in source vincolati a XContainer.

Parametri

source
IEnumerable<T>

IEnumerable<T> di XContainer che contiene la raccolta di origine.

Restituisce

IEnumerable<T> di XNode di nodi discendenti di ciascun documento ed elemento nella raccolta di origine.

Esempio

Nell'esempio seguente viene recuperata una raccolta di due elementi e quindi viene recuperata una raccolta di tutti i nodi discendenti per ogni elemento della raccolta di origine. Si noti che l'attributo dell'elemento GrandChild non viene visualizzato come nodo.

C#
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;  
    }  
}  

Nell'esempio viene prodotto l'output seguente:

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

Di seguito è riportato lo stesso esempio, ma in questo caso il codice XML si trova in uno spazio dei nomi . Per altre informazioni, vedere Usare spazi dei nomi XML.

C#
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;  
    }  
}  

Nell'esempio viene prodotto l'output seguente:

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  

Commenti

Questo metodo di estensione dell'asse viene usato su XDocument oggetti e XElement . Entrambi questi tipi derivano da XContainer, quindi questo metodo opera su un IEnumerable<T> di XContainer che contiene la raccolta di origine.

Anche se Visual Basic dispone di un asse XML integrato per gli elementi discendenti, non esiste alcun asse integrato per i nodi discendenti, pertanto gli utenti di Visual Basic devono utilizzare questo metodo dell'asse in modo esplicito.

Questo metodo usa l'esecuzione posticipata.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche