This documentation is archived and is not being maintained.

XmlDocument.CreateXmlDeclaration Method

Creates an XmlDeclaration node with the specified values.

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

public virtual XmlDeclaration CreateXmlDeclaration(
	string version,
	string encoding,
	string standalone


Type: System.String

The version must be "1.0".

Type: System.String

The value of the encoding attribute. This is the encoding that is used when you save the XmlDocument to a file or a stream; therefore, it must be set to a string supported by the Encoding class, otherwise Save fails. If this is null or String.Empty, the Save method does not write an encoding attribute on the XML declaration and therefore the default encoding, UTF-8, is used.

Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding.

Type: System.String

The value must be either "yes" or "no". If this is null or String.Empty, the Save method does not write a standalone attribute on the XML declaration.

Return Value

Type: System.Xml.XmlDeclaration
The new XmlDeclaration node.


The values of version or standalone are something other than the ones specified above.

The attributes are exposed as special properties on the XmlDeclaration node, and not as XmlAttribute nodes.

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 (, the XmlDeclaration node must be the first node in the document.

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

The following example creates an XML declaration and adds it to the document.

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

public class Sample
  public static void Main()
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +

    //Create an XML declaration. 
    XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0",null,null);

    //Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    doc.InsertBefore(xmldecl, root);

    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()
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>");

    //Create an XML declaration. 
    XmlDeclaration* xmldecl;
    xmldecl = doc->CreateXmlDeclaration(S"1.0",0,0);

    //Add the new node to the document.
    XmlElement* root = doc->DocumentElement;
    doc->InsertBefore(xmldecl, root);

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0