Extensions.Descendants Method
Definition
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 elements that contains the descendant elements of every element and document in the source collection.
Overloads
| Name | Description |
|---|---|
|
Descendants |
Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection. |
|
Descendants |
Returns a collection of elements that contains the descendant elements of every element and document in the source collection. |
Remarks
Visual Basic users can use the integrated XML descendant axis to retrieve the descendant elements of a collection. However, the integrated axis only retrieves descendants with a specified name. If Visual Basic users want to retrieve all descendants, then they must use this axis method explicitly.
This method uses deferred execution.
Descendants(IEnumerable, XName)
- Source:
- Extensions.cs
- Source:
- Extensions.cs
- Source:
- Extensions.cs
- Source:
- Extensions.cs
- Source:
- Extensions.cs
Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.
public:
generic
where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ Descendants(System::Collections::Generic::IEnumerable ^ source, System::Xml::Linq::XName ^ name);
public static System.Collections.Generic.IEnumerable Descendants(this System.Collections.Generic.IEnumerable source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable Descendants(this System.Collections.Generic.IEnumerable source, System.Xml.Linq.XName? name) where T : System.Xml.Linq.XContainer;
static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> * System.Xml.Linq.XName -> seq (requires 'T :> System.Xml.Linq.XContainer)
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T), name As XName) As IEnumerable(Of XElement)
Type Parameters
- T
The type of the objects in source, constrained to XContainer.
Parameters
- source
-
IEnumerable
An IEnumerable
Returns
An IEnumerable
Examples
The following example retrieves a collection of two elements, and then retrieves a collection of all descendants of the two elements that have the specified element name.
XElement xmlTree = XElement.Parse(
@"
This is some text
where
all of the text nodes must be concatenated.
This is a second sentence.
");
string str =
(from el in xmlTree.Elements("Para").Descendants("t")
select (string)el)
.Aggregate(new StringBuilder(),
(sb, i) => sb.Append(i),
sb => sb.ToString());
Console.WriteLine(str);
Dim xmlTree As XElement = _
This is some text
where
all of the text nodes must be concatenated.
This is a second sentence.
Dim str As String = _
( _
From el In xmlTree.... _
Select CStr(el) _
) _
.Aggregate(New StringBuilder(), _
Function(ByVal sb, ByVal i) sb.Append(i), _
Function(ByVal sb) sb.ToString())
Console.WriteLine(str)
This example produces the following output:
This is some text where all of the text nodes must be concatenated. This is a second sentence.
The following is the same example, but in this case the XML is in a namespace. For more information, see Work with XML Namespaces.
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = XElement.Parse(
@"
This is some text
where
all of the text nodes must be concatenated.
This is a second sentence.
");
string str =
(from el in xmlTree.Elements(aw + "Para").Descendants(aw + "t")
select (string)el)
.Aggregate(new StringBuilder(),
(sb, i) => sb.Append(i),
sb => sb.ToString());
Console.WriteLine(str);
Imports
Module Module1
Sub Main()
Dim xmlTree As XElement = _
This is some text
where
all of the text nodes must be concatenated.
This is a second sentence.
Dim str As String = _
( _
From el In xmlTree.... _
Select CStr(el) _
) _
.Aggregate(New StringBuilder(), _
Function(sb, i) sb.Append(i), _
Function(sb) sb.ToString())
Console.WriteLine(str)
End Sub
End Module
This example produces the following output:
This is some text where all of the text nodes must be concatenated. This is a second sentence.
Remarks
Visual Basic users can use the Language-Integrated Axes in Visual Basic (LINQ to XML) instead of using this axis method explicitly.
This method uses deferred execution.
See also
- DescendantNodesAndSelf()
- DescendantNodes()
- Descendants()
- DescendantNodes
(IEnumerable ) - Nodes
(IEnumerable ) - System.Xml.Linq.XElement.DescendantsAndSelf
- System.Xml.Linq.Extensions.DescendantsAndSelf
- LINQ to XML overview
Applies to
Descendants(IEnumerable)
- Source:
- Extensions.cs
- Source:
- Extensions.cs
- Source:
- Extensions.cs
- Source:
- Extensions.cs
- Source:
- Extensions.cs
Returns a collection of elements that contains the descendant elements of every element and document in the source collection.
public:
generic
where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ Descendants(System::Collections::Generic::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable Descendants(this System.Collections.Generic.IEnumerable source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable Descendants(this System.Collections.Generic.IEnumerable source) where T : System.Xml.Linq.XContainer;
static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq (requires 'T :> System.Xml.Linq.XContainer)
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XElement)
Type Parameters
- T
The type of the objects in source, constrained to XContainer.
Parameters
- source
-
IEnumerable
An IEnumerable
Returns
An IEnumerable
Examples
The following example retrieves a collection of elements, and then uses this axis method to retrieve all descendent elements of every item in the collection of elements.
XElement xmlTree = XElement.Parse(
@"
This is some text
where
all of the nodes must be concatenated.
This is a second sentence.
");
IEnumerable elList =
from el in xmlTree.Elements("Para").Descendants()
select el;
foreach (XElement el in elList)
Console.WriteLine(el);
Dim xmlTree As XElement = _
This is some text
where
all of the nodes must be concatenated.
This is a second sentence.
Dim elList = From el In xmlTree..Descendants _
Select el
For Each el As XElement In elList
Console.WriteLine(el)
Next
This example produces the following output:
This is some text
where
where
all of the nodes must be concatenated.
This is a second sentence.
The following is the same example, but in this case the XML is in a namespace. For more information, see Work with XML Namespaces.
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = XElement.Parse(
@"
This is some text
where
all of the nodes must be concatenated.
This is a second sentence.
");
IEnumerable elList =
from el in xmlTree.Elements(aw + "Para").Descendants()
select el;
foreach (XElement el in elList)
Console.WriteLine(el);
Imports
Module Module1
Sub Main()
Dim xmlTree As XElement = _
This is some text
where
all of the nodes must be concatenated.
This is a second sentence.
Dim elList = From el In xmlTree..Descendants _
Select el
For Each el As XElement In elList
Console.WriteLine(el)
Next
End Sub
End Module
This example produces the following output:
This is some text
where
where
all of the nodes must be concatenated.
This is a second sentence.
Remarks
Visual Basic users can use the integrated XML descendant axis to retrieve the descendant elements of a collection. However, the integrated axis only retrieves descendants with a specified name. If Visual Basic users want to retrieve all descendants, then they must use this axis method explicitly.
This method uses deferred execution.
See also
- DescendantNodesAndSelf()
- DescendantNodes()
- Descendants()
- DescendantNodes
(IEnumerable ) - Nodes
(IEnumerable ) - System.Xml.Linq.XElement.DescendantsAndSelf
- System.Xml.Linq.Extensions.DescendantsAndSelf
- LINQ to XML overview