XDocument.DocumentType 屬性

定義

取得這個文件的文件類型定義 (DTD)。

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

屬性值

XDocumentType

XDocumentType,包含這個文件的 DTD。

範例

下列範例會建立包含 XDocumentType 的檔。

Visual Basic不支援 XML 常值內的檔案類型。 不過,可以先使用 XML 常值建立檔,然後在 XML 樹狀結構的適當位置建立節點 XDocumentType ,以建立包含檔案類型的檔。

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 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 XML提供 DTD 的有限支援。

您可以使用包含 DTD 的 XML 檔填入 XML 樹狀結構。 XML 樹狀結構接著會包含 DocumentType 節點。 當您序列化或儲存樹狀結構時,也會序列化 DTD。 LINQ to XML將會展開 DTD 中的任何實體。 當您序列化或儲存 XML 樹狀結構時,不會儲存實體參考;相反地,節點會以實體的文字取代的實體參考來儲存。

如果 DTD 包含預設屬性,則會在 XML 樹狀結構中建立屬性做為一般屬性。

根據預設,LINQ to XML不會根據其 DTD 來驗證檔。 若要根據 DTD 驗證檔,請建立 XmlReader 會根據 DTD 進行驗證的 ,然後從 XmlReader 建立 XML 樹狀結構。

適用於

另請參閱