Search code examples
c#.netweb-serviceswcfcdata

How do I include a CDATA section in a WCF client webservice call in .Net?


I have been asked to create a wcf client which accesses a custom java webservice, which I can't modify. I need to consume a webservice method like:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <createDocument xmlns="http://www.dummyUrl.com/javaws">
      <version>
        ...
        <metadata>
          <attribute name="ConfigName">ConfigurationTemplateForModuleX</attribute>
          <attribute name="ConfigValue">This is a configuration string</attribute>
        </metadata>
        ...
      </version>
    </createDocument>
  </s:Body>
</s:Envelope>

Inside the attribute "ConfigValue" I usually need to save strings, but I also need to be able to save an entire XML document inside the node as CDATA like:

        ...
        <metadata>
          <attribute name="ConfigName">ConfigurationTemplateForModuleX</attribute>
          <attribute name="ConfigValue">
          <![CDATA[
            <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            <config>
               <title>Config Template for Module X</title>
               ...
            </config>
          ]]>
          </attribute>
        </metadata>
        ...

I created a service reference to my Visual Studio project to this webservice and the proxy classes were created and I can use the webservice as described in the first code section, but the problem is that the CDATA which I want to include in the request is automatically encoded and is therefore not usable anymore because I can not change the the target webservice:

 &lt;![CDATA[
   &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
   &lt;config&gt;
     &lt;title&gt;Config Template for Module X&lt;/title&gt;
     ...
   &lt;/config&gt;
 ]]&gt;

I need somehow to modify the serilization of the XML-Text attribute, or supress the encoding.

Do you have any ideas how to solve this problem?


Solution

  • Convert the XML into a Base64 string and transmit that instead. A simple re-conversion on the receiving end will give you the proper XML string.