XDocument.Root 屬性

定義

取得此文件之 XML 樹狀的根項目 (Root Element)。

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

屬性值

XML 樹狀結構的根 (Root) 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

備註

當您想要在與 撰寫樹狀結構 XElement 時相同的內容中撰寫 LINQ to XML查詢時,這個屬性非常有用。 如需詳細資訊,請參閱 查詢 XDocument 與查詢 XElement

適用於

另請參閱