title: Prefixes and Namespaces with C# Objects
body: <p>I am attempting to create a POST function that serialises C# class objects into XML.</p>
  
  <p>The part I am having great difficulty with is adding namespace prefixes to the sub-root element's children, so in this instance, <code>contact</code> children only.</p>
  
  <p>The only way I seem to be able to get the prefix onto the child elements of <code>contact</code>is to add them through <code>SerializerNamespace</code> class, however I can only get this to attach to the root element, <code>CreateContact</code>.</p>
  
  <p>How can I achieve this?</p>
  
  <p><strong>XML Currently Produced:</strong></p>
  
  <pre><code>&lt;?xml version=\"1.0\"?&gt;
  &lt;CreateContact xmlns:a="http://foo.co.uk/Contact" xmlns="http://foo.co.uk"&gt;
  &lt;a:contact&gt;
  &lt;a:Email&gt;stest@gmail.com&lt;/a:Email&gt;
  &lt;a:FirstName&gt;Simon&lt;/a:FirstName&gt;
  &lt;a:LastName&gt;Test&lt;/a:LastName&gt;
  &lt;a:Phone&gt;09088408501&lt;/a:Phone&gt;
  &lt;a:Title&gt;Mr&lt;/a:Title&gt;
  &lt;/a:contact&gt;
  &lt;/CreateContact&gt;
  </code></pre>
  
  <p><strong>Serialisation function:</strong></p>
  
  <pre><code>public static void CreateContact(Contact contact)
  {
      string tmp = url;
      string xml = "";
      string result = "";
  
      XmlDocument xd = new XmlDocument();
  
      var cc = new CreateContact();
      cc.contact = contact;
      var xs = new XmlSerializer(cc.GetType());
  
      XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
      xsn.Add("a", "http://foo.co.uk/Contact");
  
      using (MemoryStream ms = new MemoryStream())
      {
      xs.Serialize(ms, cc, xsn);
      ms.Position = 0;
      xd.Load(ms);
      xml = xd.InnerXml;
      }
  
      using (WebClient web = new WebClient())
      {
      web.Credentials = new NetworkCredential(username, password);
      web.Headers.Add("Content-Type", "application/xml");
      try
      {
          result = web.UploadString(tmp, "POST", xml);
      }
      catch (WebException ex)
      {
      }
      }
  }
  </code></pre>
  
  <p><strong>XML Class Constructs:</strong></p>
  
  <pre><code>[Serializable()]
  [XmlRoot(ElementName = "CreateContact", Namespace = "http://foo.co.uk")]
  public class CreateContact
  {
      [XmlElement(ElementName = "contact", Namespace = "http://foo.co.uk/Contact")]
      public Contact contact { get; set; }
  }
  
  [DataContract(Name = "Contact", Namespace = "http://foo.co.uk/Contact")]
  [XmlType("a")]
  public class Contact
  {
      [XmlElement(ElementName = "Email", Namespace = "http://foo.co.uk/Contact")]
      [DataMember(Name = "Email")]
      public string Email { get; set; }
      [XmlElement(ElementName = "FirstName", Namespace = "http://foo.co.uk/Contact")]
      [DataMember(Name = "FirstName")]
      public string Firstname { get; set; }
      [XmlElement(ElementName = "LastName", Namespace = "http://foo.co.uk/Contact")]
      [DataMember(Name = "LastName")]
      public string Lastname { get; set; }
      [XmlElement(ElementName = "Phone", Namespace = "http://foo.co.uk/Contact")]
      [DataMember(Name = "Phone")]
      public string Phone { get; set; }
      [XmlElement(ElementName = "Title", Namespace = "http://foo.co.uk/Contact")]
      [DataMember(Name = "Title")]
      public string Title { get; set; }
  }
  </code></pre>
  
  <p><strong>XML Desired:</strong></p>
  
  <pre><code>&lt;?xml version=\"1.0\"?&gt;
  &lt;CreateContact  xmlns="http://foo.co.uk"&gt;
  &lt;contact xmlns:a="http://foo.co.uk/Contact"&gt;
  &lt;a:Email&gt;stest@gmail.com&lt;/a:Email&gt;
  &lt;a:FirstName&gt;Simon&lt;/a:FirstName&gt;
  &lt;a:LastName&gt;Test&lt;/a:LastName&gt;
  &lt;a:Phone&gt;09088408501&lt;/a:Phone&gt;
  &lt;a:Title&gt;Mr&lt;/a:Title&gt;
  &lt;/contact&gt;
  &lt;/CreateContact&gt;
  </code></pre>