XDocument.DocumentType 屬性

定義

取得這個文件的文件類型定義 (DTD)。Gets the Document Type Definition (DTD) for this document.

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

屬性值

XDocumentType,包含這個文件的 DTD。A XDocumentType that contains the DTD for this document.

範例

下列範例會建立包含 XDocumentType的檔。The following example creates a document that contains an XDocumentType.

Visual Basic 不支援 XML 常值中的檔案類型。Visual Basic does not support document types within XML literals. 不過,您可以建立包含檔案類型的檔,方法是先使用 XML 常值建立檔,然後在 XML 樹狀結構中的適當位置建立並加入 XDocumentType 節點。However, it is possible to create a document that contains a document type by first creating the document using XML literals, and then creating and adding an XDocumentType node in the appropriate place in the XML tree.

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)  

這個範例會產生下列輸出:This example produces the following output:

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

備註

LINQ to XMLLINQ to XML 對 Dtd 提供有限的支援。provides limited support for DTDs.

您可以使用包含 DTD 的 XML 檔來填入 XML 樹狀結構。You can populate an XML tree with an XML document that contains a DTD. 接著,XML 樹狀結構會包含 DocumentType 節點。The XML tree will then contain a DocumentType node. 當您序列化或儲存樹狀結構時,DTD 也會序列化。When you serialize or save the tree, the DTD will also be serialized. LINQ to XMLLINQ to XML 會展開 DTD 中的任何實體。will expand any entities in the DTD. 當您序列化或儲存 XML 樹狀結構時,不會儲存實體參考;相反地,會使用實體的文字取代實體參考來儲存節點。When you serialize or save the XML tree, the entity references are not saved; instead, the nodes are saved with the entity references replaced by the text of the entity.

如果 DTD 包含預設屬性,則會在 XML 樹狀結構中將屬性建立為一般屬性。If the DTD contains default attributes, the attributes are created in the XML tree as ordinary attributes.

根據預設,LINQ to XMLLINQ to XML 不會依據其 DTD 來驗證檔。By default, LINQ to XMLLINQ to XML does not validate a document based on its DTD. 若要根據 DTD 驗證檔,請建立將根據 DTD 進行驗證的 XmlReader,然後從 XmlReader建立 XML 樹狀結構。To validate a document based on a DTD, create an XmlReader that will validate based on a DTD, and then create an XML tree from the XmlReader.

適用於

另請參閱