我有一个非常适用于 ASCII 的 XML 序列化程序,但是当遇到非 ASCII 字符时,它们会被替换为问号“?”。我相信我已经为 UTF8 正确配置了它,但我不确定它为什么这样做。
XmlSerializer xmls = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.NewLineChars = "\n";
settings.NewLineHandling = NewLineHandling.None;
settings.NewLineOnAttributes = false;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
{
xmls.Serialize(writer, obj, ns);
}
string xml = Encoding.UTF8.GetString(ms.ToArray());
// remove the BOM character at the beginning which screws up decoding
if (xml.Length > 0 && xml[0] != '<')
{
xml = xml.Substring(1, xml.Length - 1);
}
return xml;
}