title: Adding a Namespace to a MessageContract (WCF)
body: <p>I am trying to create a SOAP message to send to an SAP endpoint, however, I am having trouble setting the namespace correctly. I have been trying for days and tried many suggestions I found online, but none seem to work. I am hoping some of you can help me.</p>
  
  <p>This is an example of the C# code that I have:</p>
  
  <pre><code>[ServiceContract]
  [XmlSerializerFormat]
  public interface IWcfClient
  {
      [OperationContract(IsOneWay = true)]
      void SendUpdates(UpdateRequest request);
  }
  
  [MessageContract(IsWrapped = true, WrapperName = "MyRoot", WrapperNamespace = "myNamespace")]
  public class UpdateRequest
  {
      [MessageBodyMember]
      [XmlElement(ElementName = "updateType")]
      public byte UpdateType { get; internal set; }
  
      [MessageBodyMember]
      [XmlElement(ElementName = "updateEntry")]
      public UpdateEntry[] UpdateEntries { get; set; }
  }
  
  public class UpdateEntry
  {
      [XmlElement]
      public string DeviceId { get; set; }
  
      [XmlElement]
      public DateTime LastSeen { get; set; }
  }
  </code></pre>
  
  <p>This results in the SOAP body looking something like this:</p>
  
  <pre><code>&lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;MyRoot xmlns="myNamespace"&gt;
          &lt;updateEntry&gt;
              &lt;DeviceId&gt;12345&lt;/DeviceId&gt;
              &lt;LastSeen&gt;2017-04-24T14:44:30.8030649Z&lt;/LastSeen&gt;
          &lt;/updateEntry&gt;
          &lt;updateEntry&gt;
              &lt;DeviceId&gt;56789&lt;/DeviceId&gt;
              &lt;LastSeen&gt;2017-05-03T01:33:02.084Z&lt;/LastSeen&gt;
          &lt;/updateEntry&gt;
          &lt;updateType&gt;2&lt;/updateType&gt;
      &lt;/MyRoot&gt;
  &lt;/s:Body&gt;
  </code></pre>
  
  <p>What I am trying to achieve, is to add another namespace to the root and have the sub-elements use that namespace. In end effect I am looking for a result similar to this (mySecondNamespace):</p>
  
  <pre><code>&lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;MyRoot xmlns="myNamespace" xmlns:a="mySecondNamespace"&gt;
          &lt;a:updateEntry&gt;
              &lt;a:DeviceId&gt;12345&lt;/a:DeviceId&gt;
              &lt;a:LastSeen&gt;2017-04-24T14:44:30.8030649Z&lt;/a:LastSeen&gt;
          &lt;/a:updateEntry&gt;
          &lt;a:updateEntry&gt;
              &lt;a:DeviceId&gt;56789&lt;/a:DeviceId&gt;
              &lt;a:LastSeen&gt;2017-05-03T01:33:02.084Z&lt;/a:LastSeen&gt;
          &lt;/a:updateEntry&gt;
          &lt;a:updateType&gt;2&lt;/a:updateType&gt;
      &lt;/MyRoot&gt;
  &lt;/s:Body&gt;
  </code></pre>
  
  <p>I have tried:</p>
  
  <ul>
  <li>Adding XmlRoot to the MessageContract</li>
  <li>Adding XmlType to the MessageContract</li>
  <li>Using "XmlNamespaceDeclarations" attribute on a getter/setter in which the new namespace is added to the namespace collection</li>
  <li>Setting the namespace on the OperationContract</li>
  </ul>
  
  <p>But nothing seems to work. Adding the namespace to the XmlElement seems to have effect, but in that case the namespace is not on the root element and is not propagated to the underlying elements. Any ideas?</p>