xml编码特殊字符

时间:2016-06-10 15:35:38

标签: c# xml wcf rest encoding

我正在尝试发送XML rest消息,它在字段中有特殊字符。当我在解码时发送它时它会放?在特殊字符应该在哪里。

    ServiceUtil.Log(xmlRequest.OuterXml);
            TransactionFactory.Response(xmlRequest, ServiceUtil.SERVICE, ExistingReceipt.poNumber);
            ServiceUtil.Log("Written to transaction factory.");
            objHttpWebRequest = (HttpWebRequest)WebRequest.Create(BrokerDetails.endpoint);
            ServiceUtil.Log(string.Format("Endpoint {0}", BrokerDetails.endpoint));
            byte[] bytes = Encoding.ASCII.GetBytes(xmlRequest.InnerXml);
            objHttpWebRequest.Method = "POST";
            objHttpWebRequest.Headers["Authorization"] = string.Concat("Basic ", Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", BrokerDetails.username, BrokerDetails.password))));
            ServiceUtil.Log(string.Format("User {0}, password {1}", BrokerDetails.username, BrokerDetails.password));
            objHttpWebRequest.ContentLength = (long)((int)bytes.Length);
            objHttpWebRequest.ContentType = "application/xml; encoding='utf-8'";
            objRequestStream = objHttpWebRequest.GetRequestStream();
            objRequestStream.Write(bytes, 0, (int)bytes.Length);
            objRequestStream.Close();
            XmlDocument xmlDoc = new XmlDocument();
            objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
            ServiceUtil.Log(string.Format("Got status {0}", objHttpWebResponse.StatusCode.ToString()));
            if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
            {
                objXMLReader = new XmlTextReader(objHttpWebResponse.GetResponseStream());
                xmlDoc.Load(objXMLReader);
                if (xmlDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
                {
                    xmlDoc = BaseImpl.StripDeclerations(xmlDoc);
                }
                xmlDoc.LoadXml(xmlDoc.DocumentElement.OuterXml);
                objXMLReader.Close();
                TransactionFactory.Response(xmlDoc, ServiceUtil.SERVICE, ExistingReceipt.poNumber);
                Response = BaseImpl.BuildResponse<GoodsReceivedResponse> (xmlDoc);
                ServiceUtil.Log(string.Format("Response {0}", Response.Result));
                Transmited = true;
            }
            else if (Tries <= 4)
            {
                Response = null;
                Tries += 1;
            }
            else
            {
                ErrorMessages.AddAnError(new Error(string.Format(ServiceUtil.NULL_RESPONSE, ExistingReceipt.poNumber)));
                Response = new GoodsReceivedResponse(ErrorMessages.ErrorMessages);
                Transmited = true;
                ServiceUtil.Log("No response");
            }

这是编码和发送请求的代码。

    <stockReceivedRequest>
     <stockReceived>
<distributorCode>GEO</distributorCode>
<dateReceived>2016-06-10T00:00:00</dateReceived>
<timeReceived>2016-06-10T01:01:01</timeReceived>
<courier>ABC</courier>
<poNumber>1122</poNumber>
<stockDetailReceived>
  <stockDetail>
    <productCode>G0-4MK</productCode>
    <altProductCode />
    <productName>Gift set ü</productName>
    <quantity>10</quantity>
  </stockDetail>
</stockDetailReceived>

  

这是我们尝试发送的XML,这就是收到的内容。

    <?xml version="1.0"?>
    <stockReceivedRequest>
<stockReceived>
    <distributorCode>GEO</distributorCode>
    <dateReceived>2016-06-10T00:00:00</dateReceived>
    <timeReceived>2016-06-10T01:01:01</timeReceived>
    <courier>ABC</courier>
    <poNumber>1119</poNumber>
    <stockDetailReceived>
        <stockDetail>
            <productCode>G0-4MK</productCode>
            <altProductCode />
            <productName>Gift set ?</productName>
            <quantity>10</quantity>
        </stockDetail>
    </stockDetailReceived>
</stockReceived>

希望有人能够提供帮助,如果您还需要其他任何问题。

此致

1 个答案:

答案 0 :(得分:2)

Your XML has non-ASCII characters in it, but you are encoding it using ASCII. Any character that's not part of the ASCII character set will just be encoded as ?, which is what you're seeing.

Change this:

Encoding.ASCII.GetBytes(xmlRequest.InnerXml)

To this:

Encoding.UTF8.GetBytes(xmlRequest.InnerXml)

Or, better still, just save the XmlDocument directly to the stream. The default encoding for this is UTF-8:

xmlRequest.Save(objRequestStream);