XmlDocument.CreateDocumentType(String, String, String, String) 方法

定義

傳回新的 XmlDocumentType 物件。

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);
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

文件類型的名稱。

publicId
String

文件類型的公用識別項或 null。 您可以指定公用 URI 及系統識別項,以識別外部 DTD 子集的位置。

systemId
String

文件類型的系統識別項或 null。 指定外部 DTD 子集之檔案位置的 URL。

internalSubset
String

文件類型的 DTD 內部子集或 null

傳回

新的 XmlDocumentType

範例

下列範例會建立 DocumentType 節點,並將其新增至 XML 檔。

#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 集合。

雖然這個方法會在文件的內容中建立新的 物件,但不會自動將新物件新增至檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

根據 W3C Extensible Markup Language (XML) 1.0 建議,DocumentType 節點只能在 Document 節點內使用。 每個 XmlDocument 節點只能有一個 DocumentType 節點。 如果文件已經有根元素,您也必須在 (根 XmlDocument 元素之前插入 DocumentType 節點,您就無法新增 DocumentType 節點) 。 如果傳遞的參數未結合以建置有效的 XmlDocumentType,則會擲回例外狀況。

給繼承者的注意事項

這個方法具有繼承需求。 需要完全信任才能覆寫 CreateDocumentType 方法。

此方法是文件物件模型 (DOM) 的 Microsoft 延伸模組。

適用於