title: C#, XML: move second namespace to root element
body: <p>I am trying to create an XML document, using the System.XML.XmlDocument Class.</p>
  
  <p>I have two namespaces in my document.</p>
  
  <p>What my C# code looks like:</p>
  
  <pre><code>XmlDocument xDoc = new XmlDocument();
  xDoc.InsertBefore(xDoc.CreateXmlDeclaration("1.0","UTF-8","yes"),xDoc.DocumentElement);
  XmlElement root = xDoc.CreateElement('ROOT','http://example.org/ns1');
  xDoc.AppendChild(root);
  XmlElement child1 = xDoc.CreateElement('CHILD1','http://example.org/ns1');
  root.AppendChild(child1);
  XmlElement child2 = xDoc.CreateElement('ns2:CHILD2','http://example.com/ns2');
  root.AppendChild(child2);
  XmlElement child3 = xDoc.CreateElement('ns2:CHILD3','http://example.com/ns2');
  root.AppendChild(child3);
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="true"?&gt;
  &lt;ROOT xmlns="http://example.org/ns1" xmlns:ns2="http://example.com/ns2"&gt;
      &lt;CHILD1/&gt;
      &lt;ns2:CHILD2/&gt;
      &lt;ns2:CHILD3/&gt;
  &lt;/ROOT&gt;
  </code></pre>
  
  <p>Actual output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="true"?&gt;
  &lt;ROOT xmlns="http://example.org/ns1"&gt;
      &lt;CHILD1/&gt;
      &lt;ns2:CHILD2 xmlns:ns2="http://example.com/ns2"/&gt;
      &lt;ns2:CHILD3 xmlns:ns2="http://example.com/ns2"/&gt;
  &lt;/ROOT&gt;
  </code></pre>
  
  <p>Because elements of the second namespace occur multiple times in my document, I don't want this repetitive declaration of the second namespace, but instead have it only once in the root element.</p>
  
  <p>How can I achieve that?</p>
  
  <p>Using LINQ2XML is not an option for me.</p>