Windows apps
Collapse the table of content
Expand the table of content
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.

XmlWriterSettings::Encoding Property

 

Gets or sets the type of text encoding to use.

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

public:
property Encoding^ Encoding {
	Encoding^ get();
	void set(Encoding^ value);
}

Property Value

Type: System.Text::Encoding^

The text encoding to use. The default is Encoding.UTF8.

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 a StreamWriter (which derives from TextWriter) with its encoding set to UTF8, 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.

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

Do not accept an Encoding object from an untrusted source.

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

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft