2 out of 3 rated this helpful - Rate this topic

XmlDeclaration.Encoding Property

Gets or sets the encoding level of the XML document.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public string Encoding { get; set; }

Property Value

Type: System.String
The valid character encoding name. The most commonly supported character encoding names for XML are the following:

Category

Encoding Names

Unicode

UTF-8, UTF-16

ISO 10646

ISO-10646-UCS-2, ISO-10646-UCS-4

ISO 8859

ISO-8859-n (where "n" is a digit from 1 to 9)

JIS X-0208-1997

ISO-2022-JP, Shift_JIS, EUC-JP

This value is optional. If a value is not set, this property returns String.Empty.
If an encoding attribute is not included, UTF-8 encoding is assumed when the document is written or saved out.

Unlike most XML attributes, encoding attribute values are not case-sensitive. This is because encoding character names follow ISO and Internet Assigned Numbers Authority (IANA) standards.

The following example creates an XmlDeclaration node and adds it to an XML document.

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

public class Sample {

  public static void Main() {

    // Create and load the XML document.
    XmlDocument doc = new XmlDocument();
    string xmlString = "<book><title>Oberon's Legacy</title></book>";
    doc.Load(new StringReader(xmlString));

    // Create an XML declaration. 
    XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
    xmldecl.Encoding="UTF-8";
    xmldecl.Standalone="yes";     

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

    // Display the modified XML document 
    Console.WriteLine(doc.OuterXml);

  }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.