title: Serialize SOAP message with explicit namespace specification
body: <p>I have some pretty basic svcutil auto generated classes.</p>
  
  <pre><code>[System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
  [System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
  public partial class YoRequest
  {
  
      [System.ServiceModel.MessageBodyMemberAttribute(Namespace="urn:something:important", Order=0)]
      public YoInner YoInner;
  
      public YoRequest()
      {
      }
  
      public YoRequest(YoInner YoInner)
      {
          this.YoInner = YoInner;
      }
  }
  
   [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.6.1055.0")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:something:important")]
  public partial class YoInner
  {
  
      private YoClass contactField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
      public YoClass Contact
      {
          get
          {
              return this.contactField;
          }
          set
          {
              this.contactField = value;
          }
      }
  }
  
   [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.6.1055.0")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:something:important")]
  public partial class YoClass
  {
  
      private string accountIdField;
      // a bunch more fields here 
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
      public string AccountId
      {
          get
          {
              return this.accountIdField;
          }
          set
          {
              this.accountIdField = value;
          }
      }
  }
  </code></pre>
  
  <p>I tried using WCF clients but it seems under Kestrel this blocks all incoming requests until this one operation is done. Further, on Linux performance is quite pathetic. On the contrary, sending normal HTTP requests is very fast.</p>
  
  <p>So my idea was to serialize this to SOAP myself and just send to the external service.</p>
  
  <p>However, i can't for the life of me figure this one out.</p>
  
  <p>Given an instance like so:</p>
  
  <pre><code>var instance = new YoRequest
          {
              YoInner = new YoInner
              {
                  Contact = new YoClass
                  {
                      AccountId = "1234"
                  }
              }
          };
  </code></pre>
  
  <p>I need to get the following XML (and the external service is very particular, it doesn't accept inline xmlns, i need to provide this exact value)</p>
  
  <pre><code>&lt;env:Envelope xmlns:urn="urn:something:important" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;env:Header/&gt;
    &lt;env:Body&gt;
      &lt;urn:YoInner&gt;
        &lt;Contact&gt;
          &lt;AccountId&gt;1234&lt;/AccountId&gt;
        &lt;/Contact&gt;
      &lt;/urn:YoInner&gt;
    &lt;/env:Body&gt;
  &lt;/env:Envelope&gt;
  </code></pre>
  
  <p>I have hundreds of auto generated classes like <code>YoRequest</code>. I can't modify attributes on them.</p>
  
  <p>I can get the XML like so</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Envelope xmlns:urn="urn:something:important" xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;Header /&gt;
    &lt;Body d2p1:type="YoRequest" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;YoInner xmlns=""&gt;
        &lt;Contact&gt;
          &lt;AccountId&gt;1234&lt;/AccountId&gt;
        &lt;/Contact&gt;
      &lt;/YoInner&gt;
    &lt;/Body&gt;
  &lt;/Envelope&gt;
  </code></pre>
  
  <p>using the following code:</p>
  
  <pre><code> public class Header { }
  [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
      public class SoapE
      {
          [XmlElement("Header")]
          public Header Header { get; set; }
  
          [XmlElement("Body")]
          public object Body { get; set; }
      }
  
          XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
          ns.Add("urn", "urn:something:important");
          XmlSerializer mySerializer = new XmlSerializer(typeof(SoapE), new []{typeof(YoRequest)});
          var ms = new MemoryStream();
          var env = new SoapE { Body = instance, Header = new Header() };
  
          mySerializer.Serialize(ms, env, ns);
  
          var stuff = Encoding.UTF8.GetString(ms.ToArray());
         //now stuff contains the string i show above
  </code></pre>
  
  <p>However, this is not quite what i want :/ </p>