views:

115

answers:

1

I need to use ISO Latin 1 encoding, but using the code below the writer settings defaults back to UTF8. What am I missing here?

XmlDocument xmlDoc = new XmlDocument(); 
XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.Encoding = System.Text.Encoding.GetEncoding(28591);
using (XmlWriter writer = XmlWriter.Create(xmlDoc.CreateNavigato().AppendChild(), settings))
{
}
+1  A: 

The problem is that the underlying stream (in this case the xmlDoc object) is using UTF-8, which is the default encoding in .NET.

From the MSDN documentation of the Encoding property of XmlWriterSettings:

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.

In order to resolve this, you need to create your XmlWriter with a stream that is Latin-1 encoded. I don't think you can use a XmlDocument for that.

Oded