执行私有方法时无法切换到Unicode错误



你好,我收到一个错误

(-<OrderUpdateReply>-<Error><ErrorDescription>There is no Unicode byte order mark. Cannot switch to Unicode.</ErrorDescription></Error></OrderUpdateReply>

当我尝试执行这个代码块时:XSD是UFT-8-有什么想法吗?

private async Task<string> UpdateOrder(string accountName, string password, string userName)
{
string XMLstring = UpdateOrderXML(accountName, password, userName);
StringContent stringcontent = new StringContent(XMLstring);
stringcontent.Headers.ContentType.MediaType = "text/XML";
HttpResponseMessage response = await http.PostAsync("https://www.mywebsite.com/shared/xml/orderupdate.rest", stringcontent);
/*string for response*/
string ResponseString = await response.Content.ReadAsStringAsync();
XmlDocument xml = new XmlDocument();
xml.LoadXml(ResponseString);

=================================================================================响应字符串如下:我注意到XSD是UTF-8,但响应字符串是UTF-16——如何更正?

响应字符串

under return XMLstring
<?xml version="1.0" encoding="UTF-16"?>
<OrderUpdateRequestModel xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<OrderUpdate>
<OrderNo>102329</OrderNo>

<StatusUpdate>
<Status>Shipped</Status>
<TrackingNumber>1234567890</TrackingNumber>
</StatusUpdate>

<BillingStatusUpdate>
<BillingStatus>Billed</BillingStatus>
</BillingStatusUpdate>

<ShippingOptionsUpdate>
<ShipRate>25</ShipRate>
</ShippingOptionsUpdate>

<CommentsUpdate>
<CustomerComments>Sage ERP Invoice No.</CustomerComments>
</CommentsUpdate>

<SalesTaxUpdate>
<SalesTaxRate>5</SalesTaxRate>
</SalesTaxUpdate>
</OrderUpdate>
</OrderUpdateRequestModel>

xml returned on error:
<?xml version="1.0" encoding="UTF-8"?>
<OrderUpdateReply>

<Error>
<ErrorDescription>There is no Unicode byte order mark. Cannot switch to Unicode.</ErrorDescription>
</Error>
</OrderUpdateReply>
this is the header for the xsd file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="OrderUpdateRequest" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="DataTypes.xsd" />
<xs:element name="OrderUpdateRequest">

错误消息为

没有Unicode字节顺序标记。无法切换到Unicode

如果未准确指定字符编码,则无法将输入中的字节转换为字符。请参阅没有Unicode字节顺序标记。无法切换到Unicode

重要的是确定输入文档的实际字符编码。您无法通过在XML查看器、浏览器或文本编辑器中查看字符来解决此问题。唯一可靠的方法是:

  • 查看编写文档的应用程序的配置/源代码或
  • 在一个好的文本编辑器中检查文档的字节,并计算出编码是什么

输出中的UTF-16编码是由于默认StringWriter实例使用UTF-16。如果您需要UTF-8,您可以实现类似于的UTF8StringWriter

private sealed class UTF8StringWriter : StringWriter
{
public UTF8StringWriter(StringBuilder stringBuilder) : base(stringBuilder, CultureInfo.InvariantCulture)
{
}
/// <inheritdoc />
public override Encoding Encoding => new UTF8Encoding(/* UTF8 identifier or not (BOM) */);
}

并使用它来创建CCD_ 4实例:CCD_。重要信息设置的Encoding属性不用于初始化底层TextWriter,因此设置settings.Encoding = new UTF8Encoding(..)并在创建过程中传递它不会影响用XML编写的值。

相关内容

最新更新