title: Serialize classes to XML with namespaces
body: <p>I am struggling actually with serializing my classes to the desired XML. I have problems placing the namespaces in the correct way.</p>
  
  <p>here is the needed XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://abc.def.schema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;soapenv:Header&gt;
      &lt;q0:LoginScopeHeader&gt;
          &lt;organizationName&gt;WebService Test Account&lt;/organizationName&gt;
      &lt;/q0:LoginScopeHeader&gt;
      &lt;q0:SessionHeader&gt;
          &lt;sessionId&gt;00f63ba748474ebba5a5ce0f8fdf7ac4&lt;/sessionId&gt;
      &lt;/q0:SessionHeader&gt;
  &lt;/soapenv:Header&gt;
  &lt;soapenv:Body&gt;
      &lt;q0:GroupSet&gt;
          &lt;staticGroup&gt;
              &lt;number&gt;10000&lt;/number&gt;
              &lt;name&gt;Gruppe A&lt;/name&gt;
              &lt;conference&gt;false&lt;/conference&gt;
              &lt;activated&gt;true&lt;/activated&gt;
              &lt;personsCounter&gt;0&lt;/personsCounter&gt;
              &lt;messageName xsi:nil="true"/&gt;
              &lt;personNumber xsi:nil="true"/&gt;
          &lt;/staticGroup&gt;
          &lt;staticGroup&gt;
              &lt;number&gt;10000&lt;/number&gt;
              &lt;name&gt;Gruppe A&lt;/name&gt;
              &lt;conference&gt;false&lt;/conference&gt;
              &lt;activated&gt;true&lt;/activated&gt;
              &lt;personsCounter&gt;0&lt;/personsCounter&gt;
              &lt;messageName xsi:nil="true"/&gt;
              &lt;personNumber xsi:nil="true"/&gt;
          &lt;/staticGroup&gt;
      &lt;/q0:GroupSet&gt;
  &lt;/soapenv:Body&gt;
  </code></pre>
  
  <p></p>
  
  <p>Actually my class representation looks like this:</p>
  
  <pre><code>[XmlRoot(ElementName = "Envelope")]
  public class Envelope
  {
      [XmlElement(ElementName = "Header")]
      public Header Header { get; set; }
      [XmlElement(ElementName = "Body")]
      public Body Body { get; set; }
      [XmlAttribute(AttributeName = "soapenv")]
      public string Soapenv { get; set; }
      [XmlAttribute(AttributeName = "q0")]
      public string Q0 { get; set; }
      [XmlAttribute(AttributeName = "xsd")]
      public string Xsd { get; set; }
      [XmlAttribute(AttributeName = "xsi")]
      public string Xsi { get; set; }
  }
  
  [XmlRoot(ElementName = "LoginScopeHeader")]
  public class LoginScopeHeader
  {
      [XmlElement(ElementName = "organizationName")]
      public string OrganizationName { get; set; }
  }
  
  [XmlRoot(ElementName = "SessionHeader")]
  public class SessionHeader
  {
      [XmlElement(ElementName = "sessionId")]
      public string SessionId { get; set; }
  }
  
  [XmlRoot(ElementName = "Header")]
  public class Header
  {
      [XmlElement(ElementName = "LoginScopeHeader")]
      public LoginScopeHeader LoginScopeHeader { get; set; }
      [XmlElement(ElementName = "SessionHeader")]
      public SessionHeader SessionHeader { get; set; }
  }
  
  [XmlRoot(ElementName = "Body")]
  public class Body
  {
      [XmlElement(ElementName = "GroupSet")]
      public GroupSet GroupSet { get; set; }
  }
  
  [XmlRoot(ElementName = "GroupSet")]
  public class GroupSet
  {
      [XmlElement(ElementName = "staticGroup")]
      public List&lt;StaticGroup&gt; StaticGroup { get; set; }
  }
  
  [XmlRoot(ElementName = "staticGroup")]
  public class StaticGroup
  {
      [XmlElement(ElementName = "number")]
      public string Number { get; set; }
      [XmlElement(ElementName = "name")]
      public string Name { get; set; }
      [XmlElement(ElementName = "conference")]
      public string Conference { get; set; }
      [XmlElement(ElementName = "activated")]
      public string Activated { get; set; }
      [XmlElement(ElementName = "personsCounter")]
      public string PersonsCounter { get; set; }
      [XmlElement(ElementName = "messageName")]
      public MessageName MessageName { get; set; }
      [XmlElement(ElementName = "personNumber")]
      public PersonNumber PersonNumber { get; set; }
  }
  
  [XmlRoot(ElementName = "messageName")]
  public class MessageName
  {
      [XmlAttribute(AttributeName = "nil")]
      public string Nil { get; set; }
  }
  
  [XmlRoot(ElementName = "personNumber")]
  public class PersonNumber
  {
      [XmlAttribute(AttributeName = "nil")]
      public string Nil { get; set; }
  }
  </code></pre>
  
  <p>And here the extension method for the serialization:</p>
  
  <pre><code>public static string XmlSerialize&lt;T&gt;(this T item, bool removeNamespaces = true)
      {
          object locker = new object();
  
          XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
          xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
          xmlns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
          xmlns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
  
          XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
  
          XmlWriterSettings settings = new XmlWriterSettings();
          settings.Indent = true;
          settings.OmitXmlDeclaration = true;
  
          lock (locker)
          {
              StringBuilder stringBuilder = new StringBuilder();
              using (StringWriter stringWriter = new StringWriter(stringBuilder))
              {
                  using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
                  {
                      if (removeNamespaces)
                      {
                          xmlSerializer.Serialize(xmlWriter, item, xmlns);
                      }
                      else { xmlSerializer.Serialize(xmlWriter, item); }
  
                      return stringBuilder.ToString();
                  }
              }
          }
      }
  </code></pre>
  
  <p>I have actually no clue how the get the namespaces serialized like in the above XML.</p>
  
  <p>What do I miss? Any help is highly appreciated</p>