【问题标题】:Unable to encode to UTF8 with XmlWriter无法使用 XmlWriter 编码为 UTF8
【发布时间】:2018-01-04 14:49:22
【问题描述】:

我正在 WCF 中开发 Web 服务。

我想返回一个 UTF8 格式的 xml 文件。

我的代码是:

StringBuilder output = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 };
using (XmlWriter writer = XmlWriter.Create(output, settings))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("Response", Resources.nameSpaceUri);
    /* Some functions */
    writer.WriteEndElement();
    writer.WriteEndDocument();
}
return output.ToString();

但结果是这样的:

<?xml version="1.0" encoding="utf-16"?>
<Response xmlns="http://namespace">
    <!-- Content -->
</Response>

为什么我的回复是 UTF-16?

【问题讨论】:

    标签: wcf utf-8 xmlwriter


    【解决方案1】:

    这仅仅是因为 StringBuilder(以及 String、Char、char)使用 UTF-16。作者适当地声明了编码。如果您使用面向文件或流的写入器,则它使用设置中的编码。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 2016-02-07
      • 2017-04-14
      • 2011-05-22
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多