Encoding Property
.NET Framework Class Library
XmlWriterSettings..::.Encoding Property

Gets or sets the type of text encoding to use.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Visual Basic (Declaration)
Public Property Encoding As Encoding
Visual Basic (Usage)
Dim instance As XmlWriterSettings
Dim value As Encoding

value = instance.Encoding

instance.Encoding = value
C#
public Encoding Encoding { get; set; }
Visual C++
public:
property Encoding^ Encoding {
    Encoding^ get ();
    void set (Encoding^ value);
}
JScript
public function get Encoding () : Encoding
public function set Encoding (value : Encoding)

Property Value

Type: System.Text..::.Encoding
The text encoding to use. The default is Encoding.UTF-8.

The XmlWriter encodes a buffer of characters all at once, rather than character by character. An exception is thrown when the Flush method is called if any encoding errors are encountered.

The Encoding property only applies to the XmlWriter instances that are created either with the specified Stream or with the specified file name. If the XmlWriter instance is created with the specified TextWriter, the Encoding property is overridden by the encoding of the underlying TextWriter. For example, if this property is set to Unicode (UTF-16) for a particular XmlWriter, but the underlying writer is an StreamWriter (which derives from TextWriter) with its encoding set to UTF-8, the output will be UTF-8 encoded.

If the XmlWriter instance is created with other output parameters, the Encoding property is ignored.

In the following example:

  • The default value of Encoding is Encoding.UTF8.

  • The StreamWriter is set to Encoding.Unicode and the StreamWriter’s encoding overrides the Encoding setting.

  • The output will be Encoding.Unicode.

C#
using (StreamWriter output =
    new StreamWriter(new FileStream("Xml01.xml", FileMode.Create), Encoding.Unicode))
{
    using (XmlWriter xmlWriter =
        XmlWriter.Create(output, new XmlWriterSettings()))
    {
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("Root");
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
    }
}
Security noteSecurity Note:

Do not accept an Encoding object from an untrusted source.

The following example writes an XML fragment to a memory stream.

Visual Basic
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.OmitXmlDeclaration = true
settings.ConformanceLevel = ConformanceLevel.Fragment
settings.CloseOutput = false

' Create the XmlWriter object and write some content.
Dim strm as MemoryStream = new MemoryStream()
Dim writer As XmlWriter = XmlWriter.Create(strm, settings)
writer.WriteElementString("orderID", "1-456-ab")
writer.WriteElementString("orderID", "2-36-00a")
writer.Flush()
writer.Close()

' Do additonal processing on the stream.

C#
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

// Create the XmlWriter object and write some content.
MemoryStream strm = new MemoryStream();
XmlWriter writer = XmlWriter.Create(strm, settings);
writer.WriteElementString("orderID", "1-456-ab");
writer.WriteElementString("orderID", "2-36-00a");
writer.Flush();
writer.Close();

// Do additonal processing on the stream.

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

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker