David Thielen wrote:
I do the following:
StringBuilder xml = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.GetEncoding("iso-8859-1");
xws.Indent = true;
XmlWriter xtw = XmlTextWriter.Create(new StringWriter(xml), xws);
...
But the xml it creates has encoding="utf-16"
Any idea what I'm missing?
I think StringWriter always has UTF-16 as its encoding as .NET strings
are internally UTF-16 encoded.
If you really need a string declaring a different encoding then I think
you can do it as follows:
public class StringWriterWithEncoding : StringWriter {
private Encoding myEncoding;
public StringWriterWithEncoding (Encoding encoding) : base () {
myEncoding = encoding;
}
public override Encoding Encoding
{
get
{
return myEncoding;
}
}
}
then create e.g.
StringWriter stringWriter = new
StringWriterWithEncoding(Encoding.GetEncoding("ISO-8859-1"));
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteElementString("root", "Kibo");
xmlWriter.WriteEndDocument();
}
Console.WriteLine(stringWriter.ToString());
that way the result declares e.g. ISO-8859-1.
But encoding does only matter for streams not for strings so the above
is often not necessary, if you write directly to a stream then the
encoding should be taken from XmlWriterSettings.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/