XElement.AncestorsAndSelf 方法

定義

傳回包含這個元素的元素集合,以及這個元素的上階。

多載

AncestorsAndSelf()

傳回包含這個元素的元素集合,以及這個元素的上階。

AncestorsAndSelf(XName)

傳回包含這個元素的已篩選元素集合,以及這個元素的上階。 集合中只會包含具有相符之 XName 的項目。

備註

傳回集合中的專案會以反向檔順序排列。

這個方法會使用延後的執行。

AncestorsAndSelf()

傳回包含這個元素的元素集合,以及這個元素的上階。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ AncestorsAndSelf();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf ();
member this.AncestorsAndSelf : unit -> seq<System.Xml.Linq.XElement>
Public Function AncestorsAndSelf () As IEnumerable(Of XElement)

傳回

IEnumerable<XElement>

項目之 IEnumerable<T>XElement,包含這個項目及這個項目的祖系。

範例

下列範例會建立 XML 樹狀結構。 然後它會 GrandChild 尋找 元素,然後列印其上階。

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild", "element content")  
    )  
);  
XElement gc = xmlTree.Element("Child").Element("GrandChild");  
IEnumerable<XElement> aas =  
    from el in gc.AncestorsAndSelf()  
    select el;  
foreach (XElement el in aas)  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
    <Root>  
        <Child>  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  

Dim GC As XElement = xmlTree.<Child>.<GrandChild>(0)  

Dim aas As IEnumerable(Of XElement) = _  
    From el In GC.AncestorsAndSelf() _  
    Select el  

For Each el In aas  
    Console.WriteLine(el.Name)  
Next  

這個範例會產生下列輸出:

GrandChild  
Child  
Root  

備註

傳回集合中的專案會以反向檔順序排列。

這個方法會使用延後的執行。

另請參閱

適用於

AncestorsAndSelf(XName)

傳回包含這個元素的已篩選元素集合,以及這個元素的上階。 集合中只會包含具有相符之 XName 的項目。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ AncestorsAndSelf(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf (System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf (System.Xml.Linq.XName? name);
member this.AncestorsAndSelf : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function AncestorsAndSelf (name As XName) As IEnumerable(Of XElement)

參數

name
XName

要比對的 XName

傳回

IEnumerable<XElement>

IEnumerable<T>XElement,包含這個項目及這個項目的祖系。 集合中只會包含具有相符之 XName 的項目。

範例

下列範例會使用這個 。

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild", "element content")  
    )  
);  
XElement gc = xmlTree.Element("Child").Element("GrandChild");  
IEnumerable<XElement> aas = gc.AncestorsAndSelf("Child");  
foreach (XElement el in aas)  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
    <Root>  
        <Child>  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  

Dim GC As XElement = xmlTree.<Child>.<GrandChild>(0)  
Dim aas As IEnumerable(Of XElement) = GC.AncestorsAndSelf("Child")  
For Each el In aas  
    Console.WriteLine(el.Name)  
Next  

這個範例會產生下列輸出:

Child  

備註

傳回集合中的專案會以反向檔順序排列。

這個方法會使用延後的執行。

另請參閱

適用於