防止 WCF 在响应中编码字符串?

问题描述:

是否可以覆盖 WCF 中的设置以防止响应字符串中的 HTML 编码字符?

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

我有一个由第三方工具通过 SOAP 调用调用的简单服务.响应对象有一个由包含 XML 的字符串组成的属性.当 WCF 打包响应时,它变成:

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.

如果你想返回 XML 而不是字符串,那么你可以使用 XElementXmlElement作为您的返回类型.在这种情况下,它不会被编码为字符串.

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.