Condividi tramite


XDocument.DocumentType Proprietà

Definizione

Ottiene la definizione del tipo di documento (DTD) per questo documento.

public:
 property System::Xml::Linq::XDocumentType ^ DocumentType { System::Xml::Linq::XDocumentType ^ get(); };
public System.Xml.Linq.XDocumentType DocumentType { get; }
public System.Xml.Linq.XDocumentType? DocumentType { get; }
member this.DocumentType : System.Xml.Linq.XDocumentType
Public ReadOnly Property DocumentType As XDocumentType

Valore della proprietà

Oggetto XDocumentType contenente il DTD per questo documento.

Esempio

Nell'esempio seguente viene creato un documento contenente un oggetto XDocumentType.

Visual Basic non supporta i tipi di documento all'interno di valori letterali XML. Tuttavia, è possibile creare un documento contenente un tipo di documento creando prima il documento utilizzando valori letterali XML e quindi creando e aggiungendo un XDocumentType nodo nella posizione appropriata nell'albero XML.

string internalSubset = @"<!ELEMENT Pubs (Book+)>
<!ELEMENT Book (Title, Author)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>";

string target = "xml-stylesheet";
string data = "href='mystyle.css' title='Compact' type='text/css'";

XDocument doc = new XDocument(
    new XComment("This is a comment."),
    new XProcessingInstruction(target, data),
    new XDocumentType("Pubs", null, null, internalSubset),
    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.")
);
doc.Declaration = new XDeclaration("1.0", "utf-8", "true");
Console.WriteLine(doc);

doc.Save("test.xml");
Dim internalSubset = _
        "<!ELEMENT Pubs (Book+)>" & Environment.NewLine & _
        "<!ELEMENT Book (Title, Author)>" & Environment.NewLine & _
        "<!ELEMENT Title (#PCDATA)>" & Environment.NewLine & _
        "<!ELEMENT Author (#PCDATA)>"

Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <!--This is a comment.-->
    <?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
    <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>
    </Pubs>
    <!--This is another comment.-->

doc.Nodes().Skip(1).First().AddAfterSelf(New XDocumentType("Pubs", Nothing, Nothing, internalSubset))
Console.WriteLine(doc)

In questo esempio viene generato l'output seguente:

<!--This is a comment.-->
<?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
<!DOCTYPE Pubs [<!ELEMENT Pubs (Book+)>
<!ELEMENT Book (Title, Author)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>]>
<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>
</Pubs>
<!--This is another comment.-->

Commenti

LINQ to XML offre un supporto limitato per IDD.

È possibile popolare un albero XML con un documento XML contenente un DTD. L'albero XML conterrà quindi un DocumentType nodo. Quando si serializza o si salva l'albero, verrà serializzato anche il DTD. LINQ to XML espanderà tutte le entità nel DTD. Quando si serializza o si salva l'albero XML, i riferimenti all'entità non vengono salvati; I nodi vengono invece salvati con i riferimenti all'entità sostituiti dal testo dell'entità.

Se il DTD contiene attributi predefiniti, gli attributi vengono creati nell'albero XML come attributi ordinari.

Per impostazione predefinita, LINQ to XML non convalida un documento in base al relativo DTD. Per convalidare un documento basato su un DTD, creare un oggetto XmlReader che convaliderà in base a un DTD e quindi creare un albero XML da XmlReader.

Si applica a

Vedi anche