how to append node to a specific place in xml file
<p>I have an XML file like this, in it there is more than one table and I am trying to add a new node at the end of a specific table in the xml file. The user will choose the table and enter his data into a <code>DataGridView</code>.</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;table name="emloyees"&gt;
    &lt;emloyees&gt;
        &lt;emp_num&gt;employee 1&lt;/emp_num&gt;
        &lt;department&gt;sales&lt;/department&gt;
        &lt;salary&gt;1000&lt;/salary&gt;
    &lt;/employees&gt;
    &lt;employees&gt;
        &lt;emp_num&gt;employee 2&lt;/emp_num&gt;
        &lt;department&gt;IT&lt;/department&gt;
        &lt;salary&gt;2000&lt;/salary&gt;
    &lt;/employees&gt;

    ((for example I want to add new employees node here))

    &lt;table name="projects"&gt;
        &lt;projects&gt;
            &lt;proj_num&gt;project 1&lt;/proj_num&gt;
            &lt;name&gt;hosbital system&lt;/name&gt;
            &lt;num_mempers&gt;5 members&lt;/num_mempers&gt;
        &lt;/projects&gt;
        &lt;projects&gt;
        &lt;proj_num&gt;project 2&lt;/proj_num&gt;
            &lt;name&gt;library system&lt;/name&gt;
            &lt;num_mempers&gt;4 members&lt;/num_mempers&gt;
        &lt;/projects&gt;
   &lt;/table&gt;
&lt;/table&gt;
</code></pre>

<p>I wrote this code, but instead of inserting a new node, the last node of the table gets replaced with the new node instead. How do I fix this?</p>

<pre><code>private void button4_Click(object sender, EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("Data.xml");
    string tablename = comboBox1.SelectedItem.ToString();
    XmlNodeList row = doc.GetElementsByTagName(tablename);
    int c = row.Count;
    for (int j = 0; j &lt; dataGridView1.Rows.Count - 1; j++)
    {
        XmlNodeList child = row[c - 1].ChildNodes;
        XmlElement element = doc.CreateElement(tablename);
        for (int k = 0; k &lt; child.Count; k++)
        {
            XmlElement node = doc.CreateElement(child[k].ToString());
            child[k].InnerText = dataGridView1.Rows[j].Cells[k].Value.ToString();
            element.AppendChild(node);
        }
        XmlElement root = doc.DocumentElement;
        root.AppendChild(element);
        doc.Save("Data.xml");
    }
    dataGridView1.Rows.Clear();
    MessageBox.Show("Successfully Added !!");
}
</code></pre>