tags:

views:

451

answers:

2

Environment is VS2008, .Net 3.5

The following C# code (note the specified encoding of UTF8)

XmlWriterSettings settings = new XmlWriterSettings ();
StringBuilder sb  = new StringBuilder();
settings.Encoding = System.Text.Encoding.UTF8;
settings.Indent   = false;
settings.NewLineChars = "\n";
settings.ConformanceLevel =  System.Xml.ConformanceLevel.Document;

XmlWriter writer = XmlWriter.Create (sb, settings);
{
   // Write XML data.
   writer.WriteStartElement ("CCHEADER");
   writer.WriteAttributeString ("ProtocolVersion", "1.0.0");
   writer.WriteAttributeString ("ServerCapabilities", "0x0000000F");
   writer.WriteEndElement ();
   writer.Flush ();
}

Actually generates the XML (>< omitted because SO barfs on them):

?xml version="1.0" encoding="utf-16"?
CCHEADER ProtocolVersion="1.0.0" ServerCapabilities="0x0000000F" /

Why do I get the wrong encoding generated here ? What am I doing wrong ?

+7  A: 

I suspect it's because it's writing to a StringBuilder, which is inherently UTF-16. An alternative to get round this is to create a class derived from StringWriter, but which overrides the Encoding property.

I believe I've got one in MiscUtil - but it's pretty trivial to write anyway. Something 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; }
    }
}
Jon Skeet
Damn you're fast, Jon. :)
Steven Behnke
@Steven: Oh, I don't know. The question had been up for about 4 minutes. That counts as positively stale by SO standards...
Jon Skeet
Aha - followed the link - you're an MVP. I should have known. I spent 8 years as a WinSDK MVP myself. That knowledge is fast becoming stale as I struggle with this managed stuff.
Bob Moore
+1  A: 

A .Net String is encoded in Unicode (UTF-16). I expect this is the source of your encoding problems because you're writing to a StringBuilder.

Steven Behnke