Condividi tramite


XDocument.Root Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

Syntax

'Declaration
Public ReadOnly Property Root As XElement
public XElement Root { get; }

Property Value

Type: System.Xml.Linq.XElement
The root XElement of the XML tree.

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. For more information, see Querying an XDocument vs. Querying an XElement in the .NET Framework documentation.

Examples

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

Dim output As New StringBuilder
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>

output.Append(doc.Root.Name.ToString())
output.Append(Environment.NewLine)


OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
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.")
);
output.Append(doc.Root.Name.ToString() + Environment.NewLine);

OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.