XContainer.Descendants 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳該文件或元素的後代元素集合,依文件順序排列。
多載
| 名稱 | Description |
|---|---|
| Descendants() |
回傳該文件或元素的後代元素集合,依文件順序排列。 |
| Descendants(XName) |
回傳該文件或元素後裔元素的篩選集合,依文件順序排列。 集合中只會包含具有相符 XName 的專案。 |
備註
此方法使用延遲執行。
Descendants()
回傳該文件或元素的後代元素集合,依文件順序排列。
public:
System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants();
member this.Descendants : unit -> seq<System.Xml.Linq.XElement>
Public Function Descendants () As IEnumerable(Of XElement)
傳回
IEnumerable<T> XElement包含 的 後裔元素XContainer的 。
範例
以下範例建立一棵 XML 樹,並使用此軸方法取得後代。
XElement xmlTree = new XElement("Root",
new XAttribute("Att1", "AttributeContent"),
new XElement("Child",
new XText("Some text"),
new XElement("GrandChild", "element content")
)
);
IEnumerable<XElement> de =
from el in xmlTree.Descendants()
select el;
foreach (XElement el in de)
Console.WriteLine(el.Name);
' Attributes are not nodes, so will not be returned by DescendantNodes.
Dim xmlTree As XElement = _
<Root Att1="AttributeContent">
<Child>Some text
<GrandChild>element content</GrandChild>
</Child>
</Root>
Dim de = From el In xmlTree.Descendants _
Select el
For Each el In de
Console.WriteLine(el.Name)
Next
此範例會產生下列輸出:
Child
GrandChild
備註
請注意,此方法不會在結果 IEnumerable<T>中回傳自身。 看看 DescendantsAndSelf 你是否需要在結果中包含電流 XElement 。
此方法使用延遲執行。
另請參閱
適用於
Descendants(XName)
回傳該文件或元素後裔元素的篩選集合,依文件順序排列。 集合中只會包含具有相符 XName 的專案。
public:
System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName? name);
member this.Descendants : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Descendants (name As XName) As IEnumerable(Of XElement)
參數
傳回
IEnumerable<T> XElement包含與指定 XContainer相符的後代元素XName的 。
範例
以下範例列印了該元素的所有後代。
// Attributes are not nodes, so will not be returned by DescendantNodes.
XElement xmlTree = new XElement("Root",
new XAttribute("Att1", "AttributeContent"),
new XElement("Child",
new XText("Some text"),
new XElement("GrandChild", "element content")
)
);
IEnumerable<XElement> de =
from el in xmlTree.Descendants("Child")
select el;
foreach (XElement el in de)
Console.WriteLine(el.Name);
' Attributes are not nodes, so will not be returned by the descendants axis.
Dim xmlTree As XElement = _
<Root Att1="AttributeContent">
<Child>Some text
<GrandChild>element content</GrandChild>
</Child>
</Root>
Dim de = From el In xmlTree...<Child> _
Select el
For Each el In de
Console.WriteLine(el.Name)
Next
此範例會產生下列輸出:
Child
以下是相同的範例,但此例中 XML 位於命名空間中。 欲了解更多資訊,請參閱 XML 命名空間工作。
// Attributes are not nodes, so will not be returned by DescendantNodes.
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
new XAttribute(aw + "Att1", "AttributeContent"),
new XElement(aw + "Child",
new XText("Some text"),
new XElement(aw + "GrandChild", "element content")
)
);
IEnumerable<XElement> de =
from el in xmlTree.Descendants(aw + "Child")
select el;
foreach (XElement el in de)
Console.WriteLine(el.Name);
Imports <xmlns:aw = "http://www.adventure-works.com">
Module Module1
Sub Main()
' Attributes are not nodes, so will not be returned by the descendants axis.
Dim xmlTree As XElement = _
<aw:Root aw:Att1="AttributeContent">
<aw:Child>Some text
<aw:GrandChild>element content</aw:GrandChild>
</aw:Child>
</aw:Root>
Dim de = From el In xmlTree...<aw:Child> _
Select el
For Each el In de
Console.WriteLine(el.Name)
Next
End Sub
End Module
此範例會產生下列輸出:
{http://www.adventure-works.com}Child
備註
此方法使用延遲執行。