XDocument.Root Właściwość

Definicja

Pobiera element główny drzewa XML dla tego dokumentu.

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

Wartość właściwości

Katalog główny XElement drzewa XML.

Przykłady

W poniższym przykładzie użyto tej właściwości do pobrania elementu głównego dokumentu.

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())

Ten przykład generuje następujące wyniki:

Pubs

Uwagi

Ta właściwość jest przydatna, gdy chcesz utworzyć LINQ to XML zapytania w tym samym kontekście, co podczas tworzenia ich dla drzewa zakorzenionego w elemXElement. Aby uzyskać więcej informacji, zobacz Query an XDocument vs. query an XElement (Wykonywanie zapytań względem elementu XElement).

Dotyczy

Zobacz też