XElement.AncestorsAndSelf 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回元素集合,其中包含此元素及其上级。
重载
| 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<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)
	参数
返回
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  
    	注解
返回集合中的元素按反向文档顺序排列。
此方法使用延迟执行。