Use UTF-8 encoding for StringWriter in C#
All .NET string is in Unicode (UTF-16) encoding format.
So when you are using StringWriter to create your XML it will use UTF-16 encoding.
Example when creating XML :
var serializer = new XmlSerializer(yourModel.GetType(), string.Empty);
var stringBuilder = new StringBuilder();
using (var stringWriter = new StringWriter())
{
serializer.Serialize(stringWriter, yourModel);
stringBuilder.Append(stringWriter);
}
var resultString = stringBuilder.ToString();
But you want your XML to use UTF-8 encoding. One good way of doing this is to derive the StringWriter class and override the encoding.
using System.IO;
using System.Text;
public sealed class ExtentedStringWriter : StringWriter
{
private readonly Encoding stringWriterEncoding;
public ExtentedStringWriter(StringBuilder builder, Encoding desiredEncoding)
: base(builder)
{
this.stringWriterEncoding = desiredEncoding;
}
public override Encoding Encoding
{
get
{
return this.stringWriterEncoding;
}
}
}
Then you can use ExtendedStringWriter
instead of StringWriter
in the above example.