Nodes(T) Method

Extensions.Nodes<T> Method (IEnumerable<T>)

 

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

Namespace:   System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public static IEnumerable<XNode> Nodes<T>(
	this IEnumerable<T> source
)
where T : XContainer

Parameters

source
Type: System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> of XNode that contains the source collection.

Return Value

Type: System.Collections.Generic.IEnumerable<XNode>

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

Type Parameters

T

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

This method uses deferred execution.

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(
    @"<Root><Child>aaa<GrandChild>Text</GrandChild>bbb</Child>" +
    @"<Child>ccc<GrandChild>Text</GrandChild>ddd</Child></Root>");
IEnumerable<XNode> 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;
    }
}

This example produces the following output:

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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft