다음을 통해 공유


XNode.Ancestors 메서드

정의

이 노드의 상위 요소 컬렉션을 반환합니다.

오버로드

Ancestors()

이 노드의 상위 요소 컬렉션을 반환합니다.

Ancestors(XName)

이 노드의 필터링된 상위 요소 컬렉션을 반환합니다. 일치하는 XName이 있는 요소만 컬렉션에 포함됩니다.

설명

필요에 따라 노드 이름을 지정하여 특정 이름의 상위 요소를 필터링할 수 있습니다.

반환된 컬렉션의 노드 순서는 문서와 반대입니다.

이 메서드는 지연된 실행을 사용합니다.

Ancestors()

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs
Source:
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)

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs
Source:
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

설명

이 메서드는 결과에서 자신을 반환하지 않습니다.

추가 정보

적용 대상