为什么XmlWriter总是输出utf-16编码? [英] Why is the XmlWriter always outputting utf-16 encoding?
本文介绍了为什么XmlWriter总是输出utf-16编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种扩展方法
public static string SerializeObject<T>(this T value)
{
var serializer = new XmlSerializer(typeof(T));
var settings = new XmlWriterSettings
{
Encoding = new UTF8Encoding(true),
Indent = false,
OmitXmlDeclaration = false,
NewLineHandling = NewLineHandling.None
};
using(var stringWriter = new StringWriter())
{
using(var xmlWriter = XmlWriter.Create(stringWriter, settings))
{
serializer.Serialize(xmlWriter, value);
}
return stringWriter.ToString();
}
}
但是每当我叫它时,它都指定了 utf-16
的编码,即<?xml version ="1.0" encoding ="utf-16"?> 代码>.我在做什么错了?
but whenever I call this it has an encoding of utf-16
specified, ie <?xml version="1.0" encoding="utf-16"?>
. What am I doing wrong?
推荐答案
字符串是UTF-16,因此写入StringWriter将始终使用UTF-16.如果这不是您想要的,请使用其他 TextWriter
派生类,并使用您喜欢的编码.
Strings are UTF-16, so writing to a StringWriter will always use UTF-16. If that's not what you want, then use some other TextWriter
derived class, with the encoding you like.
这篇关于为什么XmlWriter总是输出utf-16编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文