title: How to send "Raw XML" inside an XML soap message; C#, .Net Web Service
body: <p>(I'm using a .Net 2.0 Web Service instead of WCF, because my needs are simple and I want full Mono compatibility...)</p>
  
  <p>Ok, I'm a C#/Visual Studio novice, so I apologize for the basic question.</p>
  
  <p>The SOAP service I'm communicating with provides a WSDL, and I used it to create .Net Web Service references. So far, so good.</p>
  
  <p>However, the service expects the payload to be an XML string, like so (an example...)</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;soap:Body&gt;
      &lt;Search xmlns="http://foo.bar/bazservice/"&gt;
        &lt;session&gt;ABCDEF0123456789&lt;/session&gt;
        &lt;options&gt;
  
  &lt;![CDATA[&lt;Search&gt;
    &lt;SearchConditions&gt;
      &lt;Condition&gt;
        &lt;Field&gt;1234&lt;/Field&gt;
        &lt;Value&gt;Test Record&lt;/Value&gt;
      &lt;/Condition&gt;
    &lt;/SearchConditions&gt;
    &lt;Fields&gt;
      &lt;Field&gt;5678&lt;/Field&gt;
    &lt;/Fields&gt;
  &lt;/Search&gt;]]&gt;
  
        &lt;/options&gt;
      &lt;/Search&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>So I call my function using the generated code that visual studio was nice enough to magic for me, like this:</p>
  
  <pre class="lang-c# prettyprint-override"><code>  // Build an XML thing, and encapsulate it in a CDATA
    XElement searchQuery = this.BuildSearchXml(params, search);
    XCData cdata = new XCData(searchQuery.ToString());
  
    // XML String looks good...
    Console.WriteLine(cdata.ToString() + Environment.NewLine);
  
    // Let's send XML payload to service...
    search client = this.Connect("search");
  
    // Search() expects the second argument to be an XML string...
    var returnVal = client.Search(session, cdata.ToString(), 1);
  
    return returnVal.ToString();
  </code></pre>
  
  <p>Unfortunately, the web service glue code, somewhere in the magic that was generated, automatically urlencodes the XML string!</p>
  
  <p>When doing some tcpdump investigation, here's the payload as it's actually sent:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;soap:Body&gt;
      &lt;Search xmlns="http://foo.bar/bazservice/"&gt;
        &lt;session&gt;ABCDEF0123456789&lt;/session&gt;
        &lt;options&gt;
  
  &amp;lt;![CDATA[&amp;lt;Search&amp;gt;
    &amp;lt;SearchConditions&amp;gt;
      &amp;lt;Condition&amp;gt;
        &amp;lt;Field&amp;gt;1234&amp;lt;/Field&amp;gt;
        &amp;lt;Value&amp;gt;Test Record&amp;lt;/Value&amp;gt;
      &amp;lt;/Condition&amp;gt;
    &amp;lt;/SearchConditions&amp;gt;
    &amp;lt;Fields&amp;gt;
      &amp;lt;Field&amp;gt;5678&amp;lt;/Field&amp;gt;
    &amp;lt;/Fields&amp;gt;
  &amp;lt;/Search&amp;gt;]]&amp;gt;
  
        &lt;/options&gt;
      &lt;/Search&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>My question is this: how do I keep using the Web Reference magic as it was generated with the WSDL, but tell it to <em>not</em> URLEncode my raw XML string, but send it as is?!</p>
  
  <p>I'm quite the novice - I'm looking at the generated code (in .\Web References\MyService\Reference.cs), but I'm at a loss.</p>
  
  <p>Thanks in advance for any help in pointing me in the right direction!!!</p>
  
  <p>...</p>
  
  <p>edit: added code for Reference.cs</p>
  
  <pre class="lang-c# prettyprint-override"><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //     Runtime Version:4.0.30319.42000
  //
  //     Changes to this file may cause incorrect behavior and will be lost if
  //     the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  //
  // This source code was auto-generated by Microsoft.VSDesigner, Version 4.0.30319.42000.
  //
  #pragma warning disable 1591
  
  namespace MyService.Search {
      using System;
      using System.Web.Services;
      using System.Diagnostics;
      using System.Web.Services.Protocols;
      using System.Xml.Serialization;
      using System.ComponentModel;
  
      // ..... snip other stuff ..... //
  
      /// &lt;remarks/&gt;
      [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.6.1055.0")]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Web.Services.WebServiceBindingAttribute(Name="searchSoap", Namespace="http://foo.bar/bazservice")]
      public partial class search : System.Web.Services.Protocols.SoapHttpClientProtocol {
  
          [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://foo.bar/bazservice/Search", RequestNamespace="http://foo.bar/bazservice", ResponseNamespace="http://foo.bar/bazservice", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
          public string ExecuteSearch(string sessionToken, string searchOptions, int pageNumber) {
              object[] results = this.Invoke("Search", new object[] {
                          sessionToken,
                          searchOptions,
                          pageNumber});
              return ((string)(results[0]));
          }
  
          public void ExecuteSearchAsync(string sessionToken, string searchOptions, int pageNumber) {
              this.ExecuteSearchAsync(sessionToken, searchOptions, pageNumber, null);
          }
  
          // ..... snip other methods ..... //
      }
  }
  
  #pragma warning restore 1591
  </code></pre>