Prevent WCF from encoding strings in response?

老子叫甜甜 提交于 2019-12-11 05:27:10

问题


Is it possible to override a setting in WCF to prevent HTML encoding characters in a response string?

I have a simple service that is called by a third party tool via a SOAP call. The response object has a single property consisting of a string which will contain XML. When WCF packages up the response, it becomes:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HttpPostResponse xmlns="http://ws.lenderprise.com">
  <HttpPostResult>
    &lt;STAT xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; &gt;
      &lt;RESPONSE Attr1=&quot;1&quot; 
             Attr2=&quot;ABC&quot; 
      &lt;/RESPONSE&gt;
    &lt;/STAT&gt;
  </HttpPostResult>
</HttpPostResponse>

Is it possible to not have the contained string encoded?

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HttpPostResponse xmlns="http://ws.lenderprise.com">
  <HttpPostResult>
    <STAT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
      <RESPONSE Attr1="1" 
             Attr2="ABC"> 
      </RESPONSE>
    </STAT>
  </HttpPostResult>
</HttpPostResponse>

I understand this still leaves me on the hook for ensuring any embedded characters are handled and that this still implies considerations on the receiving end.


回答1:


If you want to return XML instead of a string, then you can use either XElement or XmlElement as your return type. In that case it will not be encoded as a string would.



来源:https://stackoverflow.com/questions/12642877/prevent-wcf-from-encoding-strings-in-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!