i\'m creating a XDocument like this:
XDocument doc = new XDocument(
new XDeclaration(\"1.0\", \"utf-8\", \"yes\"));
when i save the document li
Very good answer using inheritance, just remember to override the initializer
public class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder sb) : base (sb)
{
}
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
You will need to set the StreamWriter.Encoding
to use UTF-8 instead of Unicode (UTF-16)
Seeing as it's not a StreamWriter this answer is only left for posterity.
StringWriter
advertises itself as using UTF-16. It's easy to fix though:
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
That should be enough in your particular case. A rather more well-rounded implementation would:
StringWriter