Documentation for this section has not yet been entered.
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:
The default value of XmlWriterSettings.Encoding is Encoding.UTF8.
The System.IO.StreamWriter is set to Encoding.Unicode and the System.IO.StreamWriter’s encoding overrides the XmlWriterSettings.Encoding setting.
The output will be Encoding.Unicode.
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(); } }
Do not accept an System.Text.Encoding object from an untrusted source.