Прочетете на английски Редактиране

Споделяне чрез


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

Definition

Returns a new XmlDocumentType object.

C#
public virtual System.Xml.XmlDocumentType CreateDocumentType(string name, string? publicId, string? systemId, string? internalSubset);
C#
public virtual System.Xml.XmlDocumentType CreateDocumentType(string name, string publicId, string systemId, string internalSubset);

Parameters

name
String

Name of the document type.

publicId
String

The public identifier of the document type or null. You can specify a public URI and also a system identifier to identify the location of the external DTD subset.

systemId
String

The system identifier of the document type or null. Specifies the URL of the file location for the external DTD subset.

internalSubset
String

The DTD internal subset of the document type or null.

Returns

The new XmlDocumentType.

Examples

The following example creates a DocumentType node and adds it to an XML document.

C#
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);
  }
}

Remarks

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.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation, DocumentType nodes are only allowed within Document nodes. Each XmlDocument can have only one DocumentType node. 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). If the passed parameters do not combine to build a valid XmlDocumentType, an exception is thrown.

Notes to Inheritors

This method has an inheritance demand. Full trust is required to override the CreateDocumentType method.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to

Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1