XDocument.Root 属性 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取此文档的 XML 树的根元素。
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.XElementPublic ReadOnly Property Root As XElement属性值
XML 树的根 XElement。
示例
以下示例使用此属性获取文档的根元素。
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())
该示例产生下面的输出:
Pubs
注解
如果要在与为 中生根的树撰写 LINQ to XML 查询相同的上下文中 XElement编写 LINQ to XML 查询,此属性非常有用。 有关更多详细信息,请参阅 查询 XDocument 与查询 XElement 。