System.Xml.XmlWriterSettings.Encoding Property

Gets or sets the type of text encoding to use.

Syntax

public System.Text.Encoding Encoding { get; set; }

Value

Documentation for this section has not yet been entered.

Remarks

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

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

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

In the following example:

Example

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();
    }
}
Note:

Do not accept an System.Text.Encoding object from an untrusted source.

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0