Language

Extensions.Nodes(IEnumerable) Method

Definition

Returns a collection of the child nodes of every document and element in the source collection.

public:
generic 
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable ^ Nodes(System::Collections::Generic::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable Nodes(this System.Collections.Generic.IEnumerable source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable Nodes(this System.Collections.Generic.IEnumerable source) where T : System.Xml.Linq.XContainer;
static member Nodes : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq (requires 'T :> System.Xml.Linq.XContainer)

Public Function Nodes(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XNode)

Public Iterator Function Nodes(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XNode)

Type Parameters

T

The type of the objects in source, constrained to XContainer.

Parameters

source
IEnumerable

An IEnumerable of XNode that contains the source collection.

Returns

An IEnumerable of XNode of the child nodes of every document and element in the source collection.

Examples

The following example retrieves all of the child nodes for every node in a collection of elements with the name of Child.

XElement xmlTree = XElement.Parse(
    @"aaaTextbbb" +
    @"cccTextddd");
IEnumerable nodes = xmlTree.Elements("Child").Nodes();

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;
    }
}
Dim xmlTree As XElement = _
    
        aaaTextbbb
        cccTextddd
    

Dim nodes = xmlTree..Nodes()

' Note that XNode uses XmlNodeType, which is in the System.Xml namespace.
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)
    End Select
Next

This example produces the following output:

Text: aaa
Element: GrandChild
Text: bbb
Text: ccc
Element: GrandChild
Text: ddd

Remarks

This method uses deferred execution.

Applies to

See also