Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XmlWriter.WriteDocType Method (String, String, String, String)


When overridden in a derived class, writes the DOCTYPE declaration with the specified name and optional attributes.

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

public abstract void WriteDocType(
	string name,
	string pubid,
	string sysid,
	string subset


Type: System.String

The name of the DOCTYPE. This must be non-empty.

Type: System.String

If non-null it also writes PUBLIC "pubid" "sysid" where pubid and sysid are replaced with the value of the given arguments.

Type: System.String

If pubid is null and sysid is non-null it writes SYSTEM "sysid" where sysid is replaced with the value of this argument.

Type: System.String

If non-null it writes [subset] where subset is replaced with the value of this argument.

Exception Condition

This method was called outside the prolog (after the root element).


The value for name would result in invalid XML.


An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message “An asynchronous operation is already in progress.”

This method does not check for invalid characters in pubid, sysid or subset. It also does not check that the internal subset is well-formed.

System_CAPS_security Security Note

The XmlWriter does not validate the data that is passed to the WriteDocType method. You should not pass arbitrary data to this method.

For the asynchronous version of this method, see WriteDocTypeAsync.

The following example writes an XML file representing a book.

using System;
using System.IO;
using System.Xml;

public class Sample {

  private const string filename = "sampledata.xml";

  public static void Main() {

     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     XmlWriter writer = XmlWriter.Create(filename, settings);

     // Write the Processing Instruction node.
     String PItext="type=\"text/xsl\" href=\"book.xsl\"";
     writer.WriteProcessingInstruction("xml-stylesheet", PItext);

     // Write the DocumentType node.
     writer.WriteDocType("book", null , null, "<!ENTITY h \"hardcover\">");

     // Write a Comment node.
     writer.WriteComment("sample XML");

     // Write the root element.

     // Write the genre attribute.
     writer.WriteAttributeString("genre", "novel");

     // Write the ISBN attribute.
     writer.WriteAttributeString("ISBN", "1-8630-014");

     // Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale");

     // Write the style element.

     // Write the price.
     writer.WriteElementString("price", "19.95");

     // Write CDATA.
     writer.WriteCData("Prices 15% off!!");

     // Write the close tag for the root element.


     // Write the XML to file and close the writer.


Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft