The other day, I was trying to write a program to output an RSS feed. I used XmlWriter with StringWriter for the output. However, I noticed that the XML document consistently output an encoding of UTF16, despite setting XMLWriterSettings.Encoding to UTF-8.
In short, the solution was to create a StringWriter that supported changing the encoding. In this instance, the encoding can be passed to the constructor.
The class follows:
Code:
using System;
using System.IO;
using System.Text;
namespace Azavia.IO
{
public class StringWriterWithEncoding : StringWriter
{
private Encoding _encoding;
public StringWriterWithEncoding(Encoding encoding)
: base()
{
_encoding = encoding;
}
public override Encoding Encoding
{
get
{
return _encoding;
}
}
}
}