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.

[Visual Basic]
Public Overridable Function CreateDocumentType( _
   ByVal name As String, _
   ByVal publicId As String, _
   ByVal systemId As String, _
   ByVal internalSubset As String _
) As XmlDocumentType
[C#]
public virtual XmlDocumentType CreateDocumentType(
 string name,
 string publicId,
 string systemId,
 string internalSubset
);
[C++]
public: virtual XmlDocumentType* CreateDocumentType(
 String* name,
 String* publicId,
 String* systemId,
 String* internalSubset
);
[JScript]
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).
systemId
The system identifier of the document type or a null reference (Nothing in Visual Basic).
internalSubset
The DTD internal subset of the document type or a null reference (Nothing in Visual Basic).

Return Value

The new XmlDocumentType.

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 (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).

Example

[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>")
        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 'Main
End Class 'Sample

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

[C++] 
#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>");
    doc->AppendChild(doctype);

    //Create the root element and 
    //add it to the document.
    doc->AppendChild(doc->CreateElement(S"book"));

    Console::WriteLine(S"Display the modified XML...");
    doc->Save(Console::Out);
}

[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.

Requirements

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft