1

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can we prevent HTMLEncoding for response string parameter?
Yes, but not easily; you'd need to use a custom message formatter (the component which converts between the CLR object - in this case, a string - and the WCF message). The post at blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/… has more information about it if you want to try that route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.