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

定义

返回新 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);
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

此文档类型的名称。Name of the document type.

publicId
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

此文档类型的系统标识符或 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

此文档类型的 DTD 内部子集或 nullThe DTD internal subset of the document type or null.

返回

XmlDocumentType

新的 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

注解

返回的节点将分析 EntitiesNotations 集合。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) 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).

适用于