title: Add namespaces and prefixes to soap message header during serialization
body: <p>I am trying to generate an xml file with the following structure : </p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"&gt;
     &lt;soap-env:Header&gt;
       &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wsse curity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:mustunderstand="1"&gt;
        &lt;wsse:UsernameToken&gt;
          &lt;wsse:Username&gt;username&lt;/wsse:Username&gt;
          &lt;wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-t oken-profile-1.0#passwordtext"&gt;secret&lt;/wsse:Password&gt;
       &lt;/wsse:UsernameToken&gt;
     &lt;/wsse:Security&gt;
   &lt;/soap-env:Header&gt;
  </code></pre>
  
  <p>The xml i get is: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap-env:Envelope xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   &lt;soap-env:Header&gt;
     &lt;wsse:Security&gt;
       &lt;wsse:UsernameToken&gt;
         &lt;wsse:Username&gt;someuser&lt;/wsse:Username&gt;
         &lt;wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#passwordtext"&gt;somepass&lt;/wsse:Password&gt;
       &lt;/wsse:UsernameToken&gt;
     &lt;/wsse:Security&gt;
   &lt;/soap-env:Header&gt;
  </code></pre>
  
  <p>The classes i have are : </p>
  
  <pre><code>[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] //
  public class SoapEnvelope&lt;T&gt;
  {
      public Header Header { get; set; }
  
      [XmlElement("Body")]
      public GenericBody&lt;T&gt; GenericBody { get; set; }
  }
  
  
  [XmlRoot(ElementName = "Security", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
  public class Header
  {
      public Security Security { get; set; }
  }
  
  
   public class Security
  {
      public UsernameToken UsernameToken { get; set; }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code> public class UsernameToken
  {
      public string Username { get; set; }
  
      public Passwords Password { get; set; }
  }
  
  public class Passwords
  {
      [XmlAttribute("type")]
      public string Type { get; set; } 
      [XmlText]
      public string Passowrd { get; set; }
  }
  </code></pre>
  
  <p>The problem is that when i declare the namespaces with the class <code>XmlSerializerNamespaces</code> like this : </p>
  
  <pre><code> var serializer = new XmlSerializer(typeof(SoapEnvelope&lt;T&gt;));
  
          var memoryStream = new MemoryStream();
          var xmlSettings = new XmlWriterSettings { Indent = false, Encoding = Encoding.UTF8 };
          var soapwriter = XmlWriter.Create(memoryStream, xmlSettings);
  
          var ns = new XmlSerializerNamespaces();
          ns.Add("soap-env", "http://schemas.xmlsoap.org/soap/envelope/");
          ns.Add("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
          //ns.Add("soap", "http://schemas.xmlsoap.org/soap/envelope/");
          serializer.Serialize(soapwriter, soapEnveloped, ns);
          soapwriter.Close();
  </code></pre>
  
  <p>I get both of the namespaces in the root element, and no namespace in the element <code>Security</code>. And also how do i manage to add also the two other namespaces  at <code>Security</code> element : <code>xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:mustunderstand="1"</code>
  Have tried different ways i found while searching these days but none of them seems to work.</p>
  
  <p>Thank you.</p>