title: How to serialize multiple objects into an existing XmlDocument, without having the namespaces on each component?
body: <p>How to serialize multiple objects into a existing XmlDocument in .Net/C#?</p>
  
  <p>I have a XmlDocument, which already contains data. I have multiple objects. Now I want to serialize them one by one and add them to the XmlDocument (AppendChild). </p>
  
  <p>This is how it should be:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;project&gt;
      &lt;mySettings&gt;...&lt;/mySettings&gt;
      &lt;component_1&gt; anydata &lt;/component_1&gt;
      ...
      &lt;component_x&gt; anydata &lt;/component_x&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>When I use the XmlSerializer I get this definition for each component:</p>
  
  <pre><code>&lt;component_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      anydata
  &lt;/component_1&gt;
  </code></pre>
  
  <p>So this is what I get, when I serialize into a string and then create a XmlNode from string, which i append to my document:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;project&gt;
      &lt;mySettings&gt;...&lt;/mySettings&gt;
      &lt;component_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; anydata &lt;/component_1&gt;
      ...
      &lt;component_x xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; anydata &lt;/component_x&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I can remove the namespace by doing this:</p>
  
  <pre><code>XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  ns.Add("", "");
  StringWriter xout = new StringWriter();
  x.Serialize(xout, data, ns);
  </code></pre>
  
  <p>But then I get the namespaces on any object inside an object array. This object:</p>
  
  <pre><code>public class component_1
  {
      object[] arr;
  }
  </code></pre>
  
  <p>will be serialized to:</p>
  
  <pre><code>&lt;component_1&gt;
    &lt;objectArray&gt;
      &lt;anyType xmlns:q1="http://www.w3.org/2001/XMLSchema" d3p1:type="q1:string" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"&gt;one&lt;/anyType&gt;
      &lt;anyType xmlns:q2="http://www.w3.org/2001/XMLSchema" d3p1:type="q2:string" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"&gt;two&lt;/anyType&gt;
    &lt;/objectArray&gt;
  &lt;/component_1&gt;
  </code></pre>
  
  <p>Is it possible to add all needed namespaces, etc. to my document and then serialize objects into XmlNodes and add them to my document, without having the namespaces on each component?</p>
  
  <p>UPDATE: 
  The function test() will serialize two objects and append them to a document.
  Last line will deserialize the first object.</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Text;
  using System.Xml;
  using System.Xml.Serialization;
  ...
  
  public class Component_1
  {
      public string Value = "Component_1.Value";
      public object[] objectArray = new object[] { "one", "two" };
  }
  void test()
  {
      object[] components = new object[] { new Component_1(), new Component_1() };
  
      XmlDocument doc = new XmlDocument();
      XmlNode rootNode = doc.AppendChild(doc.CreateElement("project"));
  
      foreach (var component in components)
          rootNode.AppendChild(doc.ReadNode(XmlTextReader.Create(new StringReader(serialize(component, true)))));
  
      Console.WriteLine(doc.OuterXml);
      Console.WriteLine(deserialize&lt;Component_1&gt;(rootNode.ChildNodes[0].OuterXml).Value);
  }
  string serialize(object obj, bool namespaces)
  {
      StringBuilder sb = new StringBuilder();
      XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true });
      (new XmlSerializer(obj.GetType())).Serialize(writer, obj, namespaces ? null : new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName("", "") }));
      return sb.ToString();
  }
  T deserialize&lt;T&gt;(string xmlString)
  {
      return (T)(new XmlSerializer(typeof(T))).Deserialize(new StringReader(xmlString));
  }
  </code></pre>
  
  <p>Maybe it is possible to add namespaces to the document (rootNode) and when creating a new XmlNode from string with the function XmlDocument.ReadNode to resolve the namespaces in string by the namespaces from XmlDocument. But I don´t know how.</p>
  
  <p>UPDATE 2:<br>
  Thanks Alex Filipovici, the serialization output is exact what I wanted.</p>
  
  <pre><code>void test2()
  {
      object[] components = new object[] { new Component_1(), new Component_1() };
  
      var doc = new XmlDocument();
  
      var project = doc.AppendChild(doc.CreateElement("project"));
  
      doc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      doc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
  
      var nav = project.CreateNavigator();
  
      var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
  
      foreach (var component in components)
      {
          using (var writer = nav.AppendChild())
          {
              var serializer = new XmlSerializer(component.GetType());
              writer.WriteWhitespace("");
              serializer.Serialize(writer, component
                  , emptyNamepsaces
                  );
              writer.Close();
          }
      }
  
      foreach (XmlNode node in doc.GetElementsByTagName("anyType"))
      {
          string attributeType = "";
          foreach (XmlAttribute xmlAttribute in node.Attributes)
          {
              if (xmlAttribute.LocalName == "type")
              {
                  attributeType = xmlAttribute.Value.Split(':')[1];
              }
          }
          node.Attributes.RemoveAll();
          node.CreateNavigator().CreateAttribute("", "type", "", attributeType);
      }
      doc.Save("output.xml");
  
      Component_1 c = deserialize&lt;Component_1&gt;(project.ChildNodes[0].OuterXml);
  
      Console.WriteLine(c.objectArray[0].GetType()); // -&gt; System.Xml.XmlNode[] !
  }
  </code></pre>
  
  <p>output:  </p>
  
  <pre><code>&lt;project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Component_1&gt;
      &lt;Value&gt;Component_1.Value&lt;/Value&gt;
      &lt;objectArray&gt;
        &lt;anyType type="string"&gt;one&lt;/anyType&gt;
        &lt;anyType type="string"&gt;two&lt;/anyType&gt;
      &lt;/objectArray&gt;
    &lt;/Component_1&gt;
    &lt;Component_1&gt;
      &lt;Value&gt;Component_1.Value&lt;/Value&gt;
      &lt;objectArray&gt;
        &lt;anyType type="string"&gt;one&lt;/anyType&gt;
        &lt;anyType type="string"&gt;two&lt;/anyType&gt;
      &lt;/objectArray&gt;
    &lt;/Component_1&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>But now the deserialization with the "T desirialize(string xmlString)" function from above fails. The object array contains XmlNodes. </p>
  
  <p>Is it possible to tell XmlSerializer to use the namespaces from the project node, or do I have to insert them again?</p>