Extensions.Nodes<T>(IEnumerable<T>) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a collection of the child nodes of every document and element in the source collection.
public:
generic <typename T>
where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Xml::Linq::XNode ^> ^ Nodes(System::Collections::Generic::IEnumerable<T> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> Nodes<T>(this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> Nodes<T>(this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;
static member Nodes : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XNode> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Nodes(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XNode)
<Extension()>
Public Iterator Function Nodes(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XNode)
- T
The type of the objects in source
, constrained to XContainer.
- source
- IEnumerable<T>
An IEnumerable<T> of XNode that contains the source collection.
An IEnumerable<T> of XNode of the child nodes of every document and element in the source collection.
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;
}
}
Dim xmlTree As XElement = _
<Root>
<Child>aaa<GrandChild>Text</GrandChild>bbb</Child>
<Child>ccc<GrandChild>Text</GrandChild>ddd</Child>
</Root>
Dim nodes = xmlTree.<Child>.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
This method uses deferred execution.
Product | Versions |
---|---|
.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 |
.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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: