0

Hello I am receiving an error

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

when I try to execute this code block: the XSD is UFT-8 - any ideas?

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);

================================================================================= The response string is as follows: I note the XSD is UTF-8 but the response string is UTF-16 -- how to correct?

Response String

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">
Michele
  • 57
  • 7
  • 1
    Is this helpful ? https://stackoverflow.com/questions/29915467 – Ahmad Ibrahim Apr 20 '20 at 12:55
  • What are the contents of `ResponseString`? Can you share a [mcve] that reproduces the problem? – dbc Apr 20 '20 at 15:08
  • dbc: the response string is: - - 102329 - Shipped 1234567890 - Billed + (partial return) – Michele Apr 20 '20 at 18:50
  • Your XML string seems invalid. I uploaded it to https://www.xmlvalidation.com/ and received an error, *Content is not allowed in prolog.*. You probably copied the XML from some sort of visualizer, which corrupted it. Can you please [edit] your question and share the raw XML without any modification (as text, not as an embedded image) so we can copy it to a console app and test it -- i.e. a [mcve]? *[How do I format my code blocks?](https://meta.stackexchange.com/q/22186)* may help you to get readable XML in your question. – dbc Apr 20 '20 at 19:11
  • I copied and pssted my XML code into the xmlvalidation.com site and it is valid XML. I cannot figure out how to get the XML into the formatter. – Michele Apr 20 '20 at 19:29
  • Just [edit] your question, paste in the raw, valid XML, and and put three literal backticks above and below the XML. I'll edit your question to show an example. ... there, done. The purpose here is for you to provide a reproducible example we can run, test and debug. – dbc Apr 20 '20 at 19:38

2 Answers2

0

The error message is

There is no Unicode byte order mark. Cannot switch to Unicode

If the character encoding is not accurately specified then the bytes in the input cannot be converted to characters. See There is no Unicode byte order mark. Cannot switch to Unicode

It is essential that you determine the actual character encoding of your input document. You cannot work this out by looking at the characters in an XML viewer, or in a browser, or in a text editor. The only reliable methods are:

  • look at the configuration/source code of the application that wrote the document or
  • examine the bytes of the document in a good text editor and work out what the encoding is.
kimbert
  • 2,376
  • 1
  • 10
  • 20
0

The UTF-16 encoding in the output is due to default of StringWriter instance uses UTF-16. If you need UTF-8 you can implement a UTF8StringWriter like so

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) */);
}

and use that to create an XmlWriter instance: XmlWriter.Create(new UTF8.., _settings). IMPORTANT The Encoding property of settings isn't used to initialize the underlaying TextWriter hence settings settings.Encoding = new UTF8Encoding(..) and passing it during creation doesn't affect the value written in XML.

joacar
  • 891
  • 11
  • 28