XNode.Ancestors 方法
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回這個節點之上階元素的集合。
Ancestors() |
傳回這個節點之上階元素的集合。 |
Ancestors(XName) |
傳回這個節點的上階元素之篩選的集合。 集合中只會包含具有相符之 XName 的項目。 |
您可以選擇性地指定節點名稱,以篩選具有特定名稱的上階專案。
所傳回集合中之節點的順序為反向文件順序。
這個方法會使用延後的執行。
- 來源:
- XNode.cs
- 來源:
- XNode.cs
- 來源:
- XNode.cs
傳回這個節點之上階元素的集合。
public:
System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors();
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors();
member this.Ancestors : unit -> seq<System.Xml.Linq.XElement>
Public Function Ancestors () As IEnumerable(Of XElement)
傳回
IEnumerable<T>,屬於這個節點之祖系項目的 XElement。
範例
下列範例會使用這個方法來列舉節點的上階。
C#
XElement xmlTree = new XElement("Root",
new XElement("Child",
new XElement("GrandChild", "content")
)
);
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");
foreach (XElement el in grandChild.Ancestors())
Console.WriteLine(el.Name);
Dim xmlTree As XElement = _
<Root>
<Child>
<GrandChild>content</GrandChild>
</Child>
</Root>
Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>
For Each el In grandChild.Ancestors()
Console.WriteLine(el.Name)
Next
這個範例會產生下列輸出:
Child
Root
備註
這個方法不會在結果中傳回本身。
所傳回集合中之節點的順序為反向文件順序。
這個方法會使用延後的執行。
另請參閱
適用於
.NET 10 和其他版本
產品 | 版本 |
---|---|
.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, 10 |
.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 |
- 來源:
- XNode.cs
- 來源:
- XNode.cs
- 來源:
- XNode.cs
傳回這個節點的上階元素之篩選的集合。 集合中只會包含具有相符之 XName 的項目。
public:
System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors(System::Xml::Linq::XName ^ name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors(System.Xml.Linq.XName name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors(System.Xml.Linq.XName? name);
member this.Ancestors : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Ancestors (name As XName) As IEnumerable(Of XElement)
參數
傳回
IEnumerable<T>,屬於這個節點之祖系項目的 XElement。 集合中只會包含具有相符之 XName 的項目。
所傳回集合中之節點的順序為反向文件順序。
這個方法會使用延後的執行。
範例
下列範例會使用這個方法。
C#
XElement xmlTree = new XElement("Root",
new XElement("Child",
new XElement("GrandChild", "content")
)
);
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");
foreach (XElement el in grandChild.Ancestors("Child"))
Console.WriteLine(el.Name);
Dim xmlTree As XElement = _
<Root>
<Child>
<GrandChild>content</GrandChild>
</Child>
</Root>
Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>
For Each el In grandChild.Ancestors("Child")
Console.WriteLine(el.Name)
Next
這個範例會產生下列輸出:
Child
備註
這個方法不會在結果中傳回本身。
另請參閱
適用於
.NET 10 和其他版本
產品 | 版本 |
---|---|
.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, 10 |
.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 |