XDocument.Root プロパティ

定義

このドキュメントの 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.XElement
Public 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便利です。 詳細については、「 XDocument のクエリと XElement のクエリ 」を参照してください。

適用対象

こちらもご覧ください