XObject.Parent 屬性

定義

取得這個 XElementXObjectGets the parent XElement of this XObject.

public:
 property System::Xml::Linq::XElement ^ Parent { System::Xml::Linq::XElement ^ get(); };
public System.Xml.Linq.XElement Parent { get; }
member this.Parent : System.Xml.Linq.XElement
Public ReadOnly Property Parent As XElement

屬性值

這個 XElement 的父 XObjectThe parent XElement of this XObject.

範例

下列範例會使用這個方法。The following example uses this method.

XDocument doc = new XDocument(  
    new XComment("A comment in the document."),  
    new XElement("Root",  
        new XElement("Child", "content")  
    )  
);  
XElement child = doc.Descendants("Child").First();  
XElement root = child.Parent;  
Console.WriteLine(root.Name);  
Dim doc As XDocument = _   
    <?xml version="1.0"?>  
    <!--A comment in the document.-->  
    <Root>  
        <Child>content</Child>  
    </Root>  
Dim child As XElement = doc.Descendants("Child").First()  
Dim root As XElement = child.Parent  
Console.WriteLine(root.Name)  

這個範例會產生下列輸出:This example produces the following output:

Root  

備註

如果此 XObject 沒有父元素,這個屬性會傳回 nullIf this XObject has no parent element, this property returns null.

注意

這個屬性會傳回父元素,而做為 XDocument 的子節點沒有父項目,這個屬性會傳回 nullThis property returns the parent element, and as children nodes of an XDocument have no parent element, this property returns null for them.

適用於

另請參閱