XmlDocument.CreateDocumentType(String, String, String, String) XmlDocument.CreateDocumentType(String, String, String, String) XmlDocument.CreateDocumentType(String, String, String, String) XmlDocument.CreateDocumentType(String, String, String, String) Method

定義

傳回新的 XmlDocumentType 物件。Returns a new XmlDocumentType object.

public:
 virtual System::Xml::XmlDocumentType ^ CreateDocumentType(System::String ^ name, System::String ^ publicId, System::String ^ systemId, System::String ^ internalSubset);
public virtual System.Xml.XmlDocumentType CreateDocumentType (string name, string publicId, string systemId, string internalSubset);
abstract member CreateDocumentType : string * string * string * string -> System.Xml.XmlDocumentType
override this.CreateDocumentType : string * string * string * string -> System.Xml.XmlDocumentType
Public Overridable Function CreateDocumentType (name As String, publicId As String, systemId As String, internalSubset As String) As XmlDocumentType

參數

name
String String String String

文件類型的名稱。Name of the document type.

publicId
String String String String

文件類型的公用識別項或 nullThe public identifier of the document type or null. 您可以指定公用 URI 及系統識別項,以識別外部 DTD 子集的位置。You can specify a public URI and also a system identifier to identify the location of the external DTD subset.

systemId
String String String String

文件類型的系統識別項或 nullThe system identifier of the document type or null. 指定外部 DTD 子集之檔案位置的 URL。Specifies the URL of the file location for the external DTD subset.

internalSubset
String String String String

文件類型的 DTD 內部子集或 nullThe DTD internal subset of the document type or null.

傳回

新的 XmlDocumentTypeThe new XmlDocumentType.

範例

下列範例會建立 DocumentType 節點, 並將它加入至 XML 檔。The following example creates a DocumentType node and adds it to an XML document.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   
   //Create a document type node and  
   //add it to the document.
   XmlDocumentType^ doctype;
   doctype = doc->CreateDocumentType( "book", nullptr, nullptr, "<!ELEMENT book ANY>" );
   doc->AppendChild( doctype );
   
   //Create the root element and 
   //add it to the document.
   doc->AppendChild( doc->CreateElement( "book" ) );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Create a document type node and  
    //add it to the document.
    XmlDocumentType doctype;
    doctype = doc.CreateDocumentType("book", null, null, "<!ELEMENT book ANY>");
    doc.AppendChild(doctype);

    //Create the root element and 
    //add it to the document.
    doc.AppendChild(doc.CreateElement("book"));

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        
        'Create a document type node and  
        'add it to the document.
        Dim doctype As XmlDocumentType
        doctype = doc.CreateDocumentType("book", Nothing, Nothing, "<!ELEMENT book ANY>")
        doc.AppendChild(doctype)
        
        'Create the root element and 
        'add it to the document.
        doc.AppendChild(doc.CreateElement("book"))
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

備註

傳回的節點將會Entities剖析和Notations集合。The returned node will have parsed Entities and Notations collections.

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. 若要加入新的物件, 您必須明確地呼叫其中一個節點插入方法。To add the new object, you must explicitly call one of the node insert methods.

根據 W3C可延伸標記語言 (XML) (XML) 1.0 建議事項, 只有在檔節點內才允許 DocumentType 節點。According to the W3C Extensible Markup Language (XML) 1.0 recommendation, DocumentType nodes are only allowed within Document nodes. XmlDocument個只能有一個 DocumentType 節點。Each XmlDocument can have only one DocumentType node. DocumentType 節點也必須在的XmlDocument根項目之前插入 (如果檔已經有根項目, 您就無法加入 DocumentType 節點)。The DocumentType node must also be inserted before the root element of the XmlDocument (if the document already has a root element, you cannot add a DocumentType node).
如果傳遞的參數未結合來建立有效XmlDocumentType的, 則會擲回例外狀況。If the passed parameters do not combine to build a valid XmlDocumentType, an exception is thrown.

給繼承者的注意事項

這個方法具有繼承需求。This method has an inheritance demand. 需要完全信任才能覆寫CreateDocumentType方法。Full trust is required to override the CreateDocumentType method.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

適用於