Serialize XML with different prefix
<p>I got 2 classes from 2 different XSDs, one of them is its child node, the root class has a property (xmlelement array) for the child and I need the child node to have a different prefix. This is my code:</p>
<pre><code>var xml = //this is the root xml
var nom = //this is the child node

var stream = new MemoryStream();

var xmlSerializeNomina = new XmlSerializer(typeof(ChildClass));

var xmlNameSpaceNom = new XmlSerializerNamespaces();

//Here add namespace because i need the prefix in child node
xmlNameSpaceNom.Add(&quot;childPrefix&quot;, &quot;http://www.url.com/child&quot;);
var doc = new XmlDocument();
using (var writer = new XmlTextWriter(stream, Encoding.UTF8))
{
    writer.Formatting = Formatting.Indented;

    xmlSerializeNomina.Serialize(writer, nom, xmlNameSpaceNom);

    stream.Seek(0, SeekOrigin.Begin);

    doc.Load(stream);

}
//This is the xmlelement array property
xml.Complemento.Any = new XmlElement[] { doc.ImportNode(doc.DocumentElement, true) as XmlElement };
</code></pre>
<p>Then I serialize the root:</p>
<pre><code>var xmlNameSpace = new XmlSerializerNamespaces();

xmlNameSpace.Add(&quot;rootPrefix&quot;, &quot;http://www.url.com/foo&quot;);
xmlNameSpace.Add(&quot;nsPrefix&quot;, &quot;http://www.url.com/foo&quot;);
xmlNameSpace.Add(&quot;xsi&quot;, &quot;http://www.w3.org/2001/XMLSchema-instance&quot;);

var urls += &quot;http://www.url.com/foo http://www.url.com/root.xsd http://www.url.com/url_child http://www.url.com/url_child/child.xsd&quot;;

//Here add then child namespace because i need it in root node    
xmlNameSpace.Add(&quot;childPrefix&quot;, &quot;http://www.url.com/child&quot;);

xmlNameSpace.Add(&quot;schemaLocation&quot;, urls);

var xmlSerializeFactura = new XmlSerializer(typeof(RootClass));

using (var xmlTextWriter = new XmlTextWriter(&quot;pathAndName.xml&quot;, Encoding.UTF8))
{
    xmlTextWriter.Formatting = Formatting.Indented;

    xmlSerializeFactura.Serialize(xmlTextWriter, xml, xmlNameSpace);
}
</code></pre>
<p>The XML file was created in the right format, but the child root has the namespace and I don't need it there, only in the root node.</p>
<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;rootPrefix:Comprobante xmlns:schemaLocation=&quot;http://www.url.com/foo http://www.url.com/root.xsd http://www.url.com/child http://www.url.com/child/child.xsd&quot; xmlns:nsPrefix=&quot;http://www.url.com/foo&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://www.url.com/foo http://www.url.com/root.xsd&quot; xmlns:rootPrefix=&quot;http://www.url.com/foo&quot;&gt;
  &lt;rootPrefix:tags&gt;
    Some more tags...
  &lt;/rootPrefix:tags
  &lt;rootPrefix:Complemento&gt;
    '&lt;!--' in this part is added the namespace, i don't need it here because i have already on root tag but if I don't add it this child tag haven't prefix  '--&gt;'
    &lt;childPrefix:Tag xmlns:childPrefix=&quot;http://www.url.com/child&quot;&gt;
      Some childs tags..
    &lt;/childPrefix:Tag&gt;
  &lt;/rootPrefix:Complemento&gt;
&lt;/rootPrefix:Comprobante&gt;
</code></pre>