title: Controlling WCF response format and namespaces
body: <p>I want my WCF response to have a response element with two namespaces using DataContracts, but I can't get it to work. This is what I would like the response to be:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;s:Header /&gt;
    &lt;s:Body&gt;
      &lt;ns2:TestReply xmlns="http://www.test.org/test/2007/00" xmlns:ns2="http://www.test2.org/test2/types"&gt;
        &lt;ns2:Result&gt;
          &lt;ns2:ActionSuccessful&gt;true&lt;/ns2:ActionSuccessful&gt;
        &lt;/ns2:Result&gt;
        &lt;ns2:ResultData&gt;
          &lt;ns2:Name&gt;Maikel Willemse&lt;/ns2:Name&gt;
        &lt;/ns2:ResultData&gt;
      &lt;/ns2:TestReply&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>This is the response I get (when testing with the WCF Test Client):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;s:Header /&gt;
    &lt;s:Body&gt;
      &lt;GetDataResponse xmlns="http://www.test.org/test/2007/00"&gt;
        &lt;TestReply xmlns:a="http://www.test2.org/test2/types" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
          &lt;a:Result&gt;
            &lt;a:ActionSuccessful&gt;true&lt;/a:ActionSuccessful&gt;
          &lt;/a:Result&gt;
          &lt;a:ResultData&gt;
            &lt;a:Name&gt;Maikel Willemse&lt;/a:Name&gt;
          &lt;/a:ResultData&gt;
        &lt;/TestReply&gt;
      &lt;/GetDataResponse&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>My service interface looks like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[ServiceContract(Namespace = "http://www.test.org/test/2007/00")]
  public interface IService1
  {
      [OperationContract]
      [return: MessageParameter(Name = "TestReply")]
      GetDataResponse GetData(string name);
  }
  </code></pre>
  
  <p>The service class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Service1 : IService1
  {
      public GetDataResponse GetData(string name)
      {
          return new GetDataResponse
              {
                  Result = new Result {ActionSuccessful = true},
                  ResultData = new ResultData {Name = name}
              };
      }
  }
  </code></pre>
  
  <p>And the DataContract classes are:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[DataContract(Namespace = "http://www.test2.org/test2/types")]
  public class GetDataResponse
  {
      [DataMember(Name = "Result")]
      public Result Result { get; set; }
  
      [DataMember(Name = "ResultData")]
      public ResultData ResultData { get; set; }
  }
  
  [DataContract(Namespace = "http://www.test2.org/test2/types")]
  public class Result
  {
      [DataMember(Name = "ActionSuccessful")]
      public bool ActionSuccessful { get; set; }
  }
  
  [DataContract(Namespace = "http://www.test2.org/test2/types")]
  public class ResultData
  {
      [DataMember(Name = "Name")]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>The target framework of my WCF project is .NET 4. The namespace prefixes do not have to be the same. How can I get the response in the format I want?</p>