This documentation is archived and is not being maintained.

XmlDocument.CreateDocumentType Method

Returns a new XmlDocumentType object.

[Visual Basic]
Public Overridable Function CreateDocumentType( _
   ByVal name As String, _
   ByVal publicId As String, _
   ByVal systemId As String, _
   ByVal internalSubset As String _
) As XmlDocumentType
public virtual XmlDocumentType CreateDocumentType(
 string name,
 string publicId,
 string systemId,
 string internalSubset
public: virtual XmlDocumentType* CreateDocumentType(
 String* name,
 String* publicId,
 String* systemId,
 String* internalSubset
public function CreateDocumentType(
   name : String,
 publicId : String,
 systemId : String,
 internalSubset : String
) : XmlDocumentType;


Name of the document type.
The public identifier of the document type or a null reference (Nothing in Visual Basic).
The system identifier of the document type or a null reference (Nothing in Visual Basic).
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 (, 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).


[Visual Basic, C#, C++] The following example creates a DocumentType node and adds it to an XML document.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
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>")
        'Create the root element and 
        'add it to the document.
        Console.WriteLine("Display the modified XML...")
    End Sub 'Main
End Class 'Sample

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

    //Create the root element and 
    //add it to the document.

    Console.WriteLine("Display the modified XML...");

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();

    //Create a document type node and  
    //add it to the document.
    XmlDocumentType* doctype;
    doctype = doc->CreateDocumentType(S"book", 0, 0, S"<!ELEMENT book ANY>");

    //Create the root element and 
    //add it to the document.

    Console::WriteLine(S"Display the modified XML...");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace