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.CreateXmlDeclaration Method

Creates an XmlDeclaration node with the specified values.

[Visual Basic]
Public Overridable Function CreateXmlDeclaration( _
   ByVal version As String, _
   ByVal encoding As String, _
   ByVal standalone As String _
) As XmlDeclaration
[C#]
public virtual XmlDeclaration CreateXmlDeclaration(
 string version,
 string encoding,
 string standalone
);
[C++]
public: virtual XmlDeclaration* CreateXmlDeclaration(
 String* version,
 String* encoding,
 String* standalone
);
[JScript]
public function CreateXmlDeclaration(
   version : String,
 encoding : String,
 standalone : String
) : XmlDeclaration;

Parameters

version
The version must be "1.0".
encoding
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 a null reference (Nothing in Visual Basic) 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.

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

Return Value

The new XmlDeclaration node.

Exceptions

Exception Type Condition
ArgumentException The values of version or standalone are something other than the ones specified above.

Remarks

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 (www.w3.org/TR/1998/REC-xml-19980210), the XmlDeclaration node must be the first node in the document.

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

Example

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

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>")
        
        'Create an XML declaration. 
        Dim xmldecl As XmlDeclaration
        xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
        
        'Add the new node to the document.
        Dim root As XmlElement = doc.DocumentElement
        doc.InsertBefore(xmldecl, root)
        
        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()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //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...");        
    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()
{
    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...");        
    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, .NET Compact Framework

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace | XmlDeclaration

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