3

We have a standard web service client created in VS 2012 with .NET 4.5 where the service reference is created through “Add service reference” in VS.

The service has an element defined like:

<xsd:element minOccurs="0" maxOccurs="1" name="Data" type="xsd:string" />

This element is supposed to contain CDATA with XML like:

<![CDATA[<data>somedata</data>]]>

Since this is a string, the element is serialized and sent to the service like

&lt;![CDATA[&lt;data&gt;somedata&lt;/data&gt;]] &gt;

The problem is that the web service don’t understand &lt and &gt. Since the web service is managed by a government agency this will not change anytime soon. Is there an easy way to tell the client not to escape < and >?

Update: Simplified version of how we send the request (C#):

Form form = new Form();
form.FormData = "<![CDATA[<message>data</message>]]>";
WebServiceClient client = new WebServiceClient();
var respons = client.SubmitForm(form);
Olav
  • 41
  • 2
  • 6
  • Please show us the code you use to populate that element. That code probably has something to do with how the element is populated. – John Saunders Mar 31 '15 at 16:36
  • @JohnSaunders For example like this: form.FormData = "<![CDATA[data]]>"; where form is an instance of a generated class in reference.cs (created by visual studio/svcutil when you add the service reference) and FormData is a property of type string. The escaping of < and > happens when the Form object is serialized to xml by the serializer (in this case DataContractSerializer). – Olav Apr 09 '15 at 19:50
  • That property is a string. It is being serialized as a string. The serializer has no way to know that you want to pretend it is XML. If you meant it to be XML, then the element type should be something like `xsd:any`, and the property should be of type `XNode` or something. The serializer is doing exactly what you asked it to do! – John Saunders Apr 09 '15 at 20:05
  • @JohnSaunders I know that, but there is no way we can change the web service. We are looking for a simple way to avoid the escaping of this property at the client side (where we have full control). It will probably work to implement an IClientMessageInspector and alter the request in the BeforeSendRequest method, but it is not a very nice solution. But it is probably what we will go for, I will post the solution here if it works. – Olav Apr 09 '15 at 21:07
  • See "[CDATA serialization with XMLSerializer](https://social.msdn.microsoft.com/Forums/en-US/6f59a2c0-2f70-4f19-9210-c675bbdce48d/cdata-serialization-with-xmlserializer?forum=asmxandxml)" and see if you can translate the concept to WCF. – John Saunders Apr 09 '15 at 21:27
  • This question can be solved in the same way as in the following existing question: [How do I include a CDATA section in a WCF client webservice call in .Net?](https://stackoverflow.com/questions/30773810/how-do-i-include-a-cdata-section-in-a-wcf-client-webservice-call-in-net) – Nino van der Mark Jun 08 '21 at 12:08
  • Does this answer your question? [How do I include a CDATA section in a WCF client webservice call in .Net?](https://stackoverflow.com/questions/30773810/how-do-i-include-a-cdata-section-in-a-wcf-client-webservice-call-in-net) – Nino van der Mark Jun 08 '21 at 12:08

0 Answers0