Compartir por


XDocument.DocumentType Propiedad

Definición

Obtiene la definición de tipo de documento (DTD) de este 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

Valor de propiedad

que XDocumentType contiene el DTD de este documento.

Ejemplos

En el ejemplo siguiente se crea un documento que contiene un XDocumentType.

Visual Basic no admite tipos de documento dentro de literales XML. Sin embargo, es posible crear un documento que contenga un tipo de documento creando primero el documento mediante literales XML y, a continuación, creando y agregando un XDocumentType nodo en el lugar adecuado en el árbol 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)

Este ejemplo produce el siguiente resultado:

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

Comentarios

LINQ to XML proporciona compatibilidad limitada con DTD.

Puede rellenar un árbol XML con un documento XML que contenga un DTD. A continuación, el árbol XML contendrá un DocumentType nodo. Al serializar o guardar el árbol, el DTD también se serializará. LINQ to XML expandirá todas las entidades del DTD. Al serializar o guardar el árbol XML, las referencias de entidad no se guardan; en su lugar, los nodos se guardan con las referencias de entidad reemplazadas por el texto de la entidad.

Si el DTD contiene atributos predeterminados, los atributos se crean en el árbol XML como atributos normales.

De forma predeterminada, LINQ to XML no valida un documento basado en su DTD. Para validar un documento basado en un DTD, cree un que XmlReader se validará en función de un DTD y, a continuación, cree un árbol XML a partir de XmlReader.

Se aplica a

Consulte también