title: Serialize part of xml file. Want namespace on root, not on serialized subelements
body: <p>I'm trying to make an Xml file that looks something like:</p>
  
  <pre><code>&lt;RootLevel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.MyCompany.com/MySchema.xsd"&gt;
      &lt;Level1&gt;
          &lt;Level2&gt;
          &lt;/Level2&gt;
      &lt;/Level1 &gt;
      &lt;Level1&gt;
          &lt;Level2&gt;
          &lt;/Level2&gt;
      &lt;/Level1 &gt;
      etc. repeats hundreds of times
  &lt;/RootLevel&gt;
  </code></pre>
  
  <p>I generated some classes from my xml schema definition file using the xsd.exe utility.  They look like:</p>
  
  <pre><code>[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://MyCompany.com/MySchema.xsd")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.MyCompany.com/MySchema.xsd", IsNullable = false)]
  public partial class RootLevel
  {
      private List&lt;Level1&gt; level1Field;
  
      public List&lt;Level1&gt; Level1Field 
      {
          get { return this.level1Field;}
          set {this.level1Field = value;}
      }
  }
  
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.MyCompany.com/MySchema.xsd")]
  public partial class Level1
  {
      private List&lt;Level2&gt; level2Field;
  
      public List&lt;Level2&gt; Level2Field 
      {
          get { return this.level2Field;}
          set {this.level2Field = value;}
      }
  
      /* other properties on Level1 go here*/
  }
  
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.MyCompany.com/MySchema.xsd")]
  public partial class Level2
  {
      /* properties on Level2 go here*/
  }    
  </code></pre>
  
  <p>I'm making this file by writing out the RootLevel element using XmlWriter.WriteStartElement() and then I write out the rest of the file by creating Level1 objects and serializing them with XmlSerializer.</p>
  
  <p><strong>Goal</strong></p>
  
  <p>I want the file to only have the namespace on the RootLevel element.</p>
  
  <p><strong>In case you are interested, here is what I tried so far:</strong></p>
  
  <p><strong>Starting Point</strong></p>
  
  <p>At the start, my RootLevel element did not have any namespaces.
  My Level1 and Level2 elements had namespaces.</p>
  
  <p><strong>Step 1:</strong></p>
  
  <p>I tried removing the namespaces from Level1 and Level2 elements by overriding the namespaces on the XmlTypeAttributes for classes Level1 and Level2.</p>
  
  <pre><code>XmlTypeAttribute attribute = new XmlTypeAttribute();
  attribute.Namespace = string.Empty;
  
  XmlAttributes attributes = new XmlAttributes();
  attributes.XmlType = attribute;
  
  XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  overrides.Add(typeof(Level1), attributes);
  overrides.Add(typeof(Level2), attributes);
  
  this.xmlSerializer = new XmlSerializer(typeof(Level1), overrides);
  </code></pre>
  
  <p><strong>Step 1 Result</strong></p>
  
  <p>The namespace was removed from Level2 but not from Level1.</p>
  
  <p><strong>Step 2</strong></p>
  
  <p>Added some more code to try to remove the namespace from Level1.  I tried using the namespaces parameter of the XmlSerializer.Serialize() method to use empty namespaces.  Note "level1" is an object of type "Level1".</p>
  
  <pre><code>XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
  namespaces.Add("", "");
  
  this.xmlSerializer.Serialize(this.xmlFileWriter, level1, namespaces);
  </code></pre>
  
  <p><strong>Step 2 Result</strong></p>
  
  <p>The namespace is removed from both Level1 and Level2.  So far, so good!  Now all I need to do is add the namespace stuff to RootLevel.</p>
  
  <p><strong>Step 3</strong></p>
  
  <p>Since RootLevel is not serialized, I added some XmlWriter code to try adding the namespaces to RootLevel.</p>
  
  <pre><code>string defaultNamespace = "http://www.MyCompany.com/MySchema.xsd";
  this.xmlFileWriter.WriteStartElement("RootLevel", defaultNamespace);
  this.xmlFileWriter.WriteAttributeString("xmlns", "xsi", "", "http://www.w3.org/2001/XMLSchema-instance");
  this.xmlFileWriter.WriteAttributeString("xmlns", "xsd", "", "http://www.w3.org/2001/XMLSchema");
  </code></pre>
  
  <p><strong>Step 3 Result</strong></p>
  
  <p>The namespaces were added to RootLevel.  Yay!.
  But, now every Level1 element has an <em>xmlns=""</em> attribute.  Grrr!</p>
  
  <pre><code>&lt;RootLevel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.MyCompany.com/MySchema.xsd"&gt;
      &lt;Level1 xmlns=""&gt;
          &lt;Level2&gt;
          &lt;/Level2&gt;
      &lt;/Level1 &gt;
      &lt;Level1 xmlns=""&gt;
          &lt;Level2&gt;
          &lt;/Level2&gt;
      &lt;/Level1 &gt;
      etc. repeats hundreds of times
  &lt;/RootLevel&gt;
  </code></pre>
  
  <p>So why did that happen?</p>