XNode.Ancestors 方法

定義

回傳該節點的祖先元素集合。

多載

名稱 Description
Ancestors()

回傳該節點的祖先元素集合。

Ancestors(XName)

回傳該節點祖先元素的篩選集合。 集合中只會包含具有相符 XName 的專案。

備註

可選擇性地指定節點名稱,以篩選具有特定名稱的祖先元素。

回傳集合中的節點順序為文件順序相反。

此方法使用延遲執行。

Ancestors()

來源:
XNode.cs
來源:
XNode.cs
來源:
XNode.cs
來源:
XNode.cs
來源:
XNode.cs

回傳該節點的祖先元素集合。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors();
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該節點的祖先元素之一。

範例

以下範例使用此方法來枚舉節點的祖先。

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

備註

此方法不會在結果中回傳。

回傳集合中的節點順序為文件順序相反。

此方法使用延遲執行。

另請參閱

適用於

Ancestors(XName)

來源:
XNode.cs
來源:
XNode.cs
來源:
XNode.cs
來源:
XNode.cs
來源:
XNode.cs

回傳該節點祖先元素的篩選集合。 集合中只會包含具有相符 XName 的專案。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors(System.Xml.Linq.XName name);
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)

參數

name
XName

那就是 XName 匹配的。

傳回

IEnumerable<T> XElement該節點的祖先元素之一。 集合中只會包含具有相符 XName 的專案。

回傳集合中的節點順序為文件順序相反。

此方法使用延遲執行。

範例

以下範例使用了此方法。

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

備註

此方法不會在結果中回傳。

另請參閱

適用於