XDocument.DocumentType Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan Definisi Tipe Dokumen (DTD) untuk dokumen ini.
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
Nilai Properti
Yang XDocumentType berisi DTD untuk dokumen ini.
Contoh
Contoh berikut membuat dokumen yang berisi XDocumentType.
Visual Basic tidak mendukung jenis dokumen dalam literal XML. Namun, dimungkinkan untuk membuat dokumen yang berisi jenis dokumen dengan terlebih dahulu membuat dokumen menggunakan literal XML, lalu membuat dan menambahkan simpul XDocumentType di tempat yang sesuai di pohon 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)
Contoh ini menghasilkan output berikut:
<!--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.-->
Keterangan
LINQ ke XML menyediakan dukungan terbatas untuk DTD.
Anda dapat mengisi pohon XML dengan dokumen XML yang berisi DTD. Pohon XML kemudian akan berisi simpul DocumentType . Saat Anda membuat serialisasi atau menyimpan pohon, DTD juga akan diserialisasikan. LINQ ke XML akan memperluas entitas apa pun di DTD. Saat Anda membuat serialisasi atau menyimpan pohon XML, referensi entitas tidak disimpan; sebagai gantinya, simpul disimpan dengan referensi entitas digantikan oleh teks entitas.
Jika DTD berisi atribut default, atribut dibuat di pohon XML sebagai atribut biasa.
Secara default, LINQ ke XML tidak memvalidasi dokumen berdasarkan DTD-nya. Untuk memvalidasi dokumen berdasarkan DTD, buat XmlReader yang akan memvalidasi berdasarkan DTD, lalu buat pohon XML dari XmlReader.