Extensions.Descendants 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回專案集合,其中包含來源集合中每個元素和檔的子代專案。
多載
| 名稱 | Description |
|---|---|
| Descendants<T>(IEnumerable<T>, XName) |
傳回篩選的專案集合,其中包含來源集合中每個元素和檔的子代專案。 集合中只會包含具有相符 XName 的專案。 |
| Descendants<T>(IEnumerable<T>) |
傳回專案集合,其中包含來源集合中每個元素和檔的子代專案。 |
備註
Visual Basic 使用者可以使用整合的 XML 後代軸來取得集合的後代元素。 然而,積分軸只會取得指定名稱的後代。 若 Visual Basic 使用者想取得所有後代,必須明確使用此軸方法。
此方法使用延遲執行。
Descendants<T>(IEnumerable<T>, XName)
傳回篩選的專案集合,其中包含來源集合中每個元素和檔的子代專案。 集合中只會包含具有相符 XName 的專案。
public:
generic <typename T>
where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Collections::Generic::IEnumerable<T> ^ source, System::Xml::Linq::XName ^ name);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T> source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T?> 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<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T), name As XName) As IEnumerable(Of XElement)
類型參數
- T
source物件類型 ,受限於 XContainer。
參數
- source
- IEnumerable<T>
其中 A IEnumerable<T>XContainer 包含了原始集合。
傳回
其中的一個IEnumerable<T>XElement包含了來源集合中每個元素和文件的後代元素。 集合中只會包含具有相符 XName 的專案。
範例
以下範例取出兩個元素的集合,接著取取所有具有指定元素名稱的元素後代集合。
XElement xmlTree = XElement.Parse(
@"<Root>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the text nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>");
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 = _
<Root>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the text nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>
Dim str As String = _
( _
From el In xmlTree.<Para>...<t> _
Select CStr(el) _
) _
.Aggregate(New StringBuilder(), _
Function(ByVal sb, ByVal i) sb.Append(i), _
Function(ByVal sb) sb.ToString())
Console.WriteLine(str)
此範例會產生下列輸出:
This is some text where all of the text nodes must be concatenated. This is a second sentence.
以下是相同的範例,但此例中 XML 位於命名空間中。 欲了解更多資訊,請參閱 XML 命名空間工作。
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the text nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>");
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 <xmlns="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim xmlTree As XElement = _
<Root>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the text nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>
Dim str As String = _
( _
From el In xmlTree.<Para>...<t> _
Select CStr(el) _
) _
.Aggregate(New StringBuilder(), _
Function(sb, i) sb.Append(i), _
Function(sb) sb.ToString())
Console.WriteLine(str)
End Sub
End Module
此範例會產生下列輸出:
This is some text where all of the text nodes must be concatenated. This is a second sentence.
備註
Visual Basic使用者可以在Visual Basic(LINQ 轉 XML) 中使用
此方法使用延遲執行。
另請參閱
- DescendantNodesAndSelf()
- DescendantsAndSelf()
- DescendantNodes()
- Descendants()
- DescendantNodes<T>(IEnumerable<T>)
- DescendantsAndSelf
- Nodes<T>(IEnumerable<T>)
- LINQ to XML 概觀
適用於
Descendants<T>(IEnumerable<T>)
傳回專案集合,其中包含來源集合中每個元素和檔的子代專案。
public:
generic <typename T>
where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Collections::Generic::IEnumerable<T> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T>(this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;
static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XElement)
類型參數
- T
source物件類型 ,受限於 XContainer。
參數
- source
- IEnumerable<T>
其中 A IEnumerable<T>XContainer 包含了原始集合。
傳回
其中的一個IEnumerable<T>XElement包含了來源集合中每個元素和文件的後代元素。
範例
以下範例會檢索一組元素,然後使用此軸方法檢索該元素集合中每個項目的所有後裔元素。
XElement xmlTree = XElement.Parse(
@"<Root>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>");
IEnumerable<XElement> elList =
from el in xmlTree.Elements("Para").Descendants()
select el;
foreach (XElement el in elList)
Console.WriteLine(el);
Dim xmlTree As XElement = _
<Root>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>
Dim elList = From el In xmlTree.<Para>.Descendants _
Select el
For Each el As XElement In elList
Console.WriteLine(el)
Next
此範例會產生下列輸出:
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t>where</t>
<t> all of the nodes must be concatenated. </t>
<t>This is a second sentence.</t>
以下是相同的範例,但此例中 XML 位於命名空間中。 欲了解更多資訊,請參閱 XML 命名空間工作。
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>");
IEnumerable<XElement> elList =
from el in xmlTree.Elements(aw + "Para").Descendants()
select el;
foreach (XElement el in elList)
Console.WriteLine(el);
Imports <xmlns="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim xmlTree As XElement = _
<Root>
<Para>
<t>This is some text </t>
<b>
<t>where</t>
</b>
<t> all of the nodes must be concatenated. </t>
</Para>
<Para>
<t>This is a second sentence.</t>
</Para>
</Root>
Dim elList = From el In xmlTree.<Para>.Descendants _
Select el
For Each el As XElement In elList
Console.WriteLine(el)
Next
End Sub
End Module
此範例會產生下列輸出:
<t xmlns="http://www.adventure-works.com">This is some text </t>
<b xmlns="http://www.adventure-works.com">
<t>where</t>
</b>
<t xmlns="http://www.adventure-works.com">where</t>
<t xmlns="http://www.adventure-works.com"> all of the nodes must be concatenated. </t>
<t xmlns="http://www.adventure-works.com">This is a second sentence.</t>
備註
Visual Basic 使用者可以使用整合的 XML 後代軸來取得集合的後代元素。 然而,積分軸只會取得指定名稱的後代。 若 Visual Basic 使用者想取得所有後代,必須明確使用此軸方法。
此方法使用延遲執行。
另請參閱
- DescendantNodesAndSelf()
- DescendantsAndSelf()
- DescendantNodes()
- Descendants()
- DescendantNodes<T>(IEnumerable<T>)
- DescendantsAndSelf
- Nodes<T>(IEnumerable<T>)
- LINQ to XML 概觀