XDocument.DocumentType Propriété

Définition

Obtient la définition de type de document (DTD) pour ce document.

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

Valeur de propriété

XDocumentType

XDocumentType qui contient la DTD pour ce document.

Exemples

L’exemple suivant crée un document qui contient un XDocumentType.

Visual Basic ne prend pas en charge les types de documents dans les littéraux XML. Toutefois, il est possible de créer un document qui contient un type de document en créant d’abord le document à l’aide de littéraux XML, puis en créant et en ajoutant un XDocumentType nœud à l’emplacement approprié dans l’arborescence 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)  

Cet exemple produit la sortie suivante :

<!--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.-->  

Remarques

LINQ to XML fournit une prise en charge limitée des DTD.

Vous pouvez remplir une arborescence XML avec un document XML qui contient un DTD. L’arborescence XML contient ensuite un DocumentType nœud. Lorsque vous sérialisez ou enregistrez l’arborescence, la DTD est également sérialisée. LINQ to XML développera toutes les entités dans le DTD. Lorsque vous sérialisez ou enregistrez l’arborescence XML, les références d’entité ne sont pas enregistrées ; Au lieu de cela, les nœuds sont enregistrés avec les références d’entité remplacées par le texte de l’entité.

Si la DTD contient des attributs par défaut, les attributs sont créés dans l’arborescence XML en tant qu’attributs ordinaires.

Par défaut, LINQ to XML ne valide pas un document en fonction de son DTD. Pour valider un document basé sur un DTD, créez un XmlReader document qui validera en fonction d’un DTD, puis créez une arborescence XML à partir de l’objet XmlReader.

S’applique à

Voir aussi