title: How to make a copy of a XML node with all their child nodes and values but different name C# .NET
body: <p>I'm trying to make a copy of a XML node and all their child nodes but different XML parent node name, but is throwing me an error, this is the xml file:</p>
  
  <pre><code>&lt;Servers&gt;
    &lt;MyServer&gt;
      &lt;Host&gt;0.0.0.0&lt;/Host&gt;
       &lt;Port&gt;12&lt;/Port&gt;
       &lt;User&gt;USER&lt;/User&gt;
    &lt;/MyServer&gt;
  &lt;/Servers&gt;
  </code></pre>
  
  <p>What I'm trying to do is a copy of MyServer with all their child nodes and values but different name... something like this</p>
  
  <pre><code>&lt;Servers&gt;
    &lt;MyServer&gt;
      &lt;Host&gt;0.0.0.0&lt;/Host&gt;
       &lt;Port&gt;12&lt;/Port&gt;
       &lt;User&gt;USER&lt;/User&gt;
    &lt;/MyServer&gt;
    &lt;MyCopyofMyServer&gt;
      &lt;Host&gt;0.0.0.0&lt;/Host&gt;
       &lt;Port&gt;12&lt;/Port&gt;
       &lt;User&gt;USER&lt;/User&gt;
    &lt;/MyCopyofMyServer&gt;
  &lt;/Servers&gt;
  </code></pre>
  
  <p>What I did was this:</p>
  
  <pre><code> public void CopyInterface(string NewServer, string ServerToCopy)
   {
      xmldoc.Load(XMLInterfacesFile);
      XmlNode NodeToCopy = xmldoc.SelectSingleNode("Servers/" + ServerToCopy);
      XmlNode deep = NodeToCopy.CloneNode(true);    
      deep.InnerXml = deep.InnerXml.Replace(ServerToCopy, NewServer);
      xmldoc.AppendChild(deep);   //Throwing an exception here!
      xmldoc.Save(XMLInterfacesFile);            
   }
  </code></pre>
  
  <p>Exception:  This document already has a 'DocumentElement' node.</p>
  
  <p>Any Idea?</p>