title: SOAP request with WEB API
body: <p>There is a SOAP web service and I'm accessing it with my WEB API.</p>
  <p>Here is the SOAP xml request.</p>
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:SOAPENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchemainstance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
  &lt;SOAP-ENV:Body&gt;
      &lt;UserCredential&gt;
          &lt;Username&gt;admin&lt;/Username&gt;
          &lt;Password&gt;Admin123&lt;/Password&gt;
      &lt;/UserCredential&gt;
  &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  <p>Here is the Class, that I'm using.</p>
  <pre><code>   [XmlRoot(ElementName=&quot;UserCredential&quot;)]
     public class UserCredential
     {
        [XmlElement(ElementName=&quot;Username&quot;)] 
        public string Username { get; set; } 
  
        [XmlElement(ElementName=&quot;Password&quot;)] 
        public string Password { get; set; } 
    }
  </code></pre>
  <p>API reply is something like below</p>
  <pre><code> &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;&lt;SOAP-ENV:Header/&gt;
   &lt;SOAP-ENV:Body&gt;
       &lt;UserCredentialReply&gt;
          &lt;AccessAlow&gt;true&lt;/AccessAlow&gt;
       &lt;/UserCredentialReply&gt;
   &lt;/SOAP-ENV:Body&gt;
   &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  <p>It's C# class,</p>
  <pre><code>[XmlRoot(ElementName=&quot;UserCredentialReply&quot;)]
  public class UserCredentialReply { 
  
      [XmlElement(ElementName=&quot;AccessAlow&quot;)] 
      public bool AccessAlow { get; set; } 
  }
  </code></pre>
  <p>Here I call the API,</p>
  <pre><code>using (var client = new HttpClient())
  {
      var firstLine = &quot;&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot; xmlns:SOAPENC=\&quot;http://schemas.xmlsoap.org/soap/encoding/\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchemainstance\&quot; xmlns:xsd=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&gt;&lt;SOAP-ENV:Body&gt;&quot;;
      var lastLine = &quot;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;&quot;;
  
      var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
  
      string strRequest = &quot;&quot;;
      XmlSerializer xsSubmit = new XmlSerializer(typeof(UserCredential));
      using (var strWriter = new StringWriter())
      {
          using (XmlWriter writer = XmlWriter.Create(strWriter))
          {
              xsSubmit.Serialize(writer, cred);
              strRequest = strWriter.ToString();
          }
      }
      
      strRequest = strRequest.Replace(&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-16\&quot;?&gt;&quot;, &quot;&quot;);//POINT 1
      var xmlString = firstLine + strRequest + lastLine; // POINT 2
      var content = new StringContent(xmlString, Encoding.UTF8, MediaTypeNames.Application.Xml);
      request.Content = content;
      var res = client.SendAsync(request).Result;
      
      if (res.IsSuccessStatusCode)
      {
          var returnedString = res.Content.ReadAsStringAsync().Result;
          var reply = DeserializeXml&lt;UserCredentialReply&gt;(returnedString);//POINT 3
      }
  
  }
  </code></pre>
  <p>This is how I deserialize,</p>
  <pre><code>    private T DeserializeXml&lt;T&gt;(string responseObjStr)
      {
          var serializer = new XmlSerializer(typeof(T));
          using (var stringReader = new StringReader(responseObjStr))
          {
              return (T)serializer.Deserialize(stringReader);
          }
      }
  </code></pre>
  <p><strong>POINT 1</strong> : As you can see when serializing object to the XML will generate, <code>&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;&quot;</code> part and should remove it for getting proper response. This is ugly.</p>
  <p><strong>POINT 2</strong> : SOAP-ENV tags should send along with the request for getting a successful response. This also not upto standard.</p>
  <p><strong>POINT 3</strong> : Response comes with SOAP-ENV tags. So it cannot deserialize properly.</p>
  <p>Is there any proper way to use SOAP web services with WEB API project? How can I clean the code for above 3 points?</p>