XDocument.DocumentType Proprietà

Definizione

Ottiene la definizione DTD (Document Type Definition) per il 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à

XDocumentType

XDocumentType che contiene la definizione DTD del documento.

Esempio

Nell'esempio seguente viene creato un documento contenente un XDocumentTypeoggetto .

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

Nell'esempio viene prodotto 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 I DTD.

È 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 salva l'albero XML, i riferimenti alle 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 normali.

Per impostazione predefinita, LINQ to XML non convalida un documento basato sul DTD. Per convalidare un documento basato su un DTD, creare un oggetto XmlReader che convalida in base a un DTD e quindi creare un albero XML dall'oggetto XmlReader.

Si applica a

Vedi anche