How to put an encoding attribute to xml other that utf-16 with XmlWriter?

c#, encoding, xmlwriter

Solution

You need to use a StringWriter with the appropriate encoding. Unfortunately StringWriter doesn't let you specify the encoding directly, so you need a class like this:

public sealed class StringWriterWithEncoding : StringWriter
{
    private readonly Encoding encoding;

    public StringWriterWithEncoding (Encoding encoding)
    {
        this.encoding = encoding;
    }

    public override Encoding Encoding
    {
        get { return encoding; }
    }
}

(This question is similar but not quite a duplicate.)

EDIT: To answer the comment: pass the StringWriterWithEncoding to XmlWriter.Create instead of the StringBuilder, then call ToString() on it at the end.

Problem

I've got a function creating some XmlDocument: ``` public string CreateOutputXmlString(ICollection<Field> fields) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = Encoding.GetEncoding("windows-1250"); StringBuilder builder = new StringBuilder(); XmlWriter writer = XmlWriter.Create(builder, settings); writer.WriteStartDocument(); writer.WriteStartElement("data"); foreach (Field field in fields) { writer.WriteStartElement("item"); writer.WriteAttributeString("name", field.Id); writer.WriteAttributeString("value", field.Value); writer.WriteEndElement(); } writer.WriteEndElement(); writer.Flush(); writer.Close(); return builder.ToString(); } ``` I set an encoding but after i create XmlWriter it does have utf-16 encoding. I know it's because strings (and StringBuilder i suppose) are encoded in utf-16 and you can't change it. So how can I easily create this xml with the encoding attribute set to "windows-1250"? it doesn't even have to be encoded in this encoding, it just has to have the specified attribute. edit: it has to be in .Net 2.0 so any new framework elements cannot be used.

Original source

Related problems