title: Adding multiple namespaces to MessageContract WCF response object (MessageBodyMember)
body: <p>We have a WCF setup with the following contracts:</p>
  
  <pre><code>    [ServiceContract(
      Namespace = Constants.Namespaces.HL7Namespace,
      Name = Constants.Roles.ContentRequiredDocumentManagementSystem)]
  // XmlSerializerFormat is needed to expose the HL7 schema fields without the "Field" suffix on each one, eg: idField
  [XmlSerializerFormat]
  public interface ICDARequest
  {
      [OperationContract(
          // wsdl request action
          Action = Constants.Namespaces.HL7Namespace + ":" + Constants.Interactions.RCMR_IN000029UV01 + "." + Constants.VersionType.NormativeCode + Constants.Version.InteractionVersion,
          // wsdl operation name
          Name = Constants.Interactions.RCMR_IN000029UV01,
          // wsdl response action
          ReplyAction = Constants.Namespaces.HL7Namespace + ":" + Constants.Interactions.RCMR_IN000030UV01 + "." + Constants.VersionType.NormativeCode + Constants.Version.InteractionVersion)]
      SearchMessagesResponse SearchMessages(SearchMessagesRequest RCMR_IN000029UV01);
  
  
      [MessageContract(
          IsWrapped = false]
      public class SearchMessagesResponse
      {
          [MessageBodyMember(
              Name = State.Constants.Interactions.RCMR_IN000030UV01,
              Namespace = State.Constants.Namespaces.HL7Namespace)]
          public RCMR_IN000030UV01 data;
      }
  }
  </code></pre>
  
  <ul>
  <li>These are based on classes that been generated based on an HL7v3 schema using <code>xsd.exe</code>. </li>
  <li>we then altered the schema to add a custom element using a custom namespace to differentiate it and regenerated the classes.</li>
  <li>this worked fine.</li>
  </ul>
  
  <p>It added:</p>
  
  <pre><code>[System.Xml.Serialization.XmlTypeAttribute(TypeName = "BCCDX.DistributionStatus", Namespace = "urn:bccdx.ca")]
  public partial class BCCDXDistributionStatus
  {
      [System.Xml.Serialization.XmlElementAttribute("receivedTime", Namespace = "urn:bccdx.ca", IsNullable = false)]
      public TS receivedTime{...}
  }
  </code></pre>
  
  <p>which is what was desired.</p>
  
  <p>Then in the WCF service we are able to use the new class and members:</p>
  
  <pre><code>var distStatus = new BCCDXDistributionStatus();
  distStatus.receivedTime = CreateTS(locStat.MessageDownloadDate);
  </code></pre>
  
  <p>this then gets serialized and sent out across the wire looking like:</p>
  
  <pre><code>&lt;distributionStatus xmlns="urn:bccdx.ca"&gt;
      &lt;receivedTime value="201702150956-0800"/&gt;
  &lt;/distributionStatus&gt;
  </code></pre>
  
  <p>which is almost correct. The hitch comes with the fact that the XML document has no reference to the <code>"urn:bccdx.ca"</code> namespace. I was assuming it would be automatically added to the document root element upon serialization, but I was wrong. Here's what that ends up looking like:</p>
  
  <pre><code>&lt;RCMR_IN000030UV01 ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3"&gt;
  ...
  &lt;/RCMR_IN000030UV01&gt;
  </code></pre>
  
  <p>when what is really desired is something like:</p>
  
  <pre><code>&lt;RCMR_IN000030UV01 ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3" xmlns:x="urn:bccdx.ca"&gt;
  ...
  &lt;/RCMR_IN000030UV01&gt;
  </code></pre>
  
  <p><strong>note the urn:bccdx.ca with prefix</strong></p>
  
  <p>I'm wondering how, if at all we can add more than one namespace, with prefixes to the resulting serialized message XML through the contracts? I've seen hints on the web of overriding the default serializer, but I'd rather not. Surely this has been thought of and dealt with before?</p>