why does the Xdocument give me a utf16 declaration?

后端 未结 3 2013
慢半拍i
慢半拍i 2021-02-05 02:45

i\'m creating a XDocument like this:

XDocument doc = new XDocument(
new XDeclaration(\"1.0\", \"utf-8\", \"yes\"));

when i save the document li

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 03:12

    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:

    • Have constructors matching those in StringWriter
    • Allow the encoding to be specified in the constructor too

提交回复
热议问题