Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlDocument.CreateDocumentType Method

Returns a new XmlDocumentType object.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public virtual XmlDocumentType CreateDocumentType (
	string name,
	string publicId,
	string systemId,
	string internalSubset
)
public XmlDocumentType CreateDocumentType (
	String name, 
	String publicId, 
	String systemId, 
	String internalSubset
)
public function CreateDocumentType (
	name : String, 
	publicId : String, 
	systemId : String, 
	internalSubset : String
) : XmlDocumentType

Parameters

name

Name of the document type.

publicId

The public identifier of the document type or a null reference (Nothing in Visual Basic). You can specify a public URI and also a system identifier to identify the location of the external DTD subset.

systemId

The system identifier of the document type or a null reference (Nothing in Visual Basic). Specifies the URL of the file location for the external DTD subset.

internalSubset

The DTD internal subset of the document type or a null reference (Nothing in Visual Basic).

Return Value

The new XmlDocumentType.

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 (www.w3.org/TR/1998/REC-xml-19980210), 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. See Inheritance Demands for more information. This method is a Microsoft extension to the Document Object Model (DOM).

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

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

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        //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.get_Out());
    } //main
} //Sample

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft