Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

XDocument.Root Property

Definition

Namespace:
System.Xml.Linq
Assemblies:
System.Xml.Linq.dll, System.Xml.XDocument.dll
Assemblies:
netstandard.dll, System.Xml.XDocument.dll
Assembly:
System.Xml.XDocument.dll
Assembly:
System.Xml.Linq.dll
Assembly:
netstandard.dll
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Gets the root element of the XML Tree for this document.

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

Property Value

The root XElement of the XML tree.

Examples

The following example uses this property to get the root element of a document.

XDocument doc = new XDocument(
 new XComment("This is a comment."),
 new XElement("Pubs",
 new XElement("Book",
 new XElement("Title", "Artifacts of Roman Civilization"),
 new XElement("Author", "Moreno, Jordao")
 ),
 new XElement("Book",
 new XElement("Title", "Midieval Tools and Implements"),
 new XElement("Author", "Gazit, Inbar")
 )
 ),
 new XComment("This is another comment.")
);
Console.WriteLine(doc.Root.Name.ToString());
Dim doc As XDocument = _
 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <!--This is a comment.-->
 <Pubs>
 <Book>
 <Title>Artifacts of Roman Civilization</Title>
 <Author>Moreno, Jordao</Author>
 </Book>
 <Book>
 <Title>Midieval Tools and Implements</Title>
 <Author>Gazit, Inbar</Author>
 </Book>
 <!--This is another comment.-->
 </Pubs>

Console.WriteLine(doc.Root.Name.ToString())

This example produces the following output:

Pubs

Remarks

This property is useful when you want to compose LINQ to XML queries in the same context as when composing them for a tree rooted in XElement. See Query an XDocument vs. query an XElement for more details.

Applies to

See also


Feedback

Was this page helpful?