System.Xml.XmlDocument.CreateDocumentType 方法

方法描述

返回新的 XmlDocumentType 对象。

语法定义(C# System.Xml.XmlDocument.CreateDocumentType 方法 的用法)

[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public virtual XmlDocumentType CreateDocumentType(
	string name,
	string publicId,
	string systemId,
	string internalSubset
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
name System-String 此文档类型的名称。
publicId System-String 此文档类型的公共标识符或 null。可以指定公共 URI 以及系统标识符,以标识外部 DTD 子集的位置。
systemId System-String 此文档类型的系统标识符或 null。指定外部 DTD 子集的文件位置的 URL。
internalSubset System-String 此文档类型的 DTD 内部子集或 null。
返回值 System.Xml.XmlDocumentType 新的 XmlDocumentType。

提示和注释

返回的节点将具有经过分析的 Entities 和 Notations 集合。

尽管此方法在文档的上下文中创建新对象,但它并不自动将新对象添加到文档树。 若要添加新对象,必须显式调用节点插入方法之一。

根据 W3C 可扩展标记语言 (XML) 1.0 建议 (www.w3.org/TR/1998/REC-xml-19980210),DocumentType 节点只能包含在 Document 节点中。 每个 XmlDocument 只能有一个 DocumentType 节点。 而且还必须在插入 XmlDocument 的根元素之前插入 DocumentType 节点(如果该文档已具有根元素,则不能添加 DocumentType 节点)。

如果传递的参数不合并以生成有效的 XmlDocumentType,则将引发异常。

对继承者的说明

该方法具有继承要求。 需要完全信任才能重写 CreateDocumentType 方法。 有关更多信息,请参见 继承需求。

该方法是文档对象模型 (DOM) 的 Microsoft 扩展。

System.Xml.XmlDocument.CreateDocumentType 方法例子

下面的示例创建一个 DocumentType 节点,并将其添加到 XML 文档中。

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, "");
    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);
  }
}

异常

异常 异常描述

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。