Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XObject.Parent Property

Gets the parent XElement of this XObject.

Namespace:  System.Xml.Linq
Assemblies:   System.Xml.Linq (in System.Xml.Linq.dll)
  System.Xml.XDocument (in System.Xml.XDocument.dll)

public XElement Parent { get; }

Property Value

Type: System.Xml.Linq.XElement
The parent XElement of this XObject.

If this XObject has no parent element, this property returns null.

NoteNote

This property returns the parent element, and as children nodes of an XDocument have no parent element, this property returns null for them.

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);

This example produces the following output:

Root

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft