title: How to add child node to the root element in an XML file using C#
body: <p>The xml file is having the following structure</p>
  
  <pre><code>&lt;RootElement&gt;
  &lt;/RootElement&gt;
  </code></pre>
  
  <p>i need to append the "Child" element to both RootElement and TestChild .
  For this I'm using the following code. </p>
  
  <pre><code>         List&lt;string&gt; Str = new List&lt;string&gt; {"a","b"};
          XmlDocument XDOC = new XmlDocument();
          XDOC.Load(Application.StartupPath + "\\Sample.xml");
          XmlNode RootNode = XDOC.SelectSingleNode("//RootElement");
          XmlNode TestChild = XDOC.CreateNode(XmlNodeType.Element, "TestChild", null);
          for (int Index = 0; Index &lt; Str.Count; Index++)
          {
              XmlElement XEle = XDOC.CreateElement("Child");
              XEle.SetAttribute("Name", Str[Index]);
              TestChild.AppendChild(XEle);
              RootNode.AppendChild(XEle);
          }
          RootNode.AppendChild(TestChild);
          XDOC.Save(Application.StartupPath + "\\Sample.xml");
  </code></pre>
  
  <p>But with this i can append the child node to only the RootElement</p>
  
  <p>The result should come like</p>
  
  <pre><code>    &lt;RootElement&gt;
      &lt;Child Name="a"/&gt;
      &lt;Child Name="b"/&gt;
      &lt;TestChild&gt;
          &lt;Child Name="a"/&gt;
          &lt;Child Name="b"/&gt;
      &lt;/TestChild&gt;
  &lt;/RootElement&gt;
  </code></pre>
  
  <p>But now i'm getting like</p>
  
  <pre><code>    &lt;RootElement&gt;  
          &lt;Child Name="a" /&gt;
          &lt;Child Name="b" /&gt;
          &lt;TestChild&gt;
          &lt;/TestChild&gt;
       &lt;/RootElement&gt;
  </code></pre>
  
  <p>please give me a solution to do this</p>
  
  <p>Thanks in advance</p>