Inserting new node into xml file example:

The following sample will create a new node and will be added to existing xml file.

In InsertNewNode.aspx file:

<table>
    <tr>
    <td align="right">VisitorNum:</td>
    <td><asp:TextBox ID="tbVstrNum" runat="server" ></asp:TextBox></td>
    </tr>
    <tr>
    <td align="right">FName:</td>
    <td><asp:TextBox ID="tbFName" runat="server" ></asp:TextBox></td>
    </tr>
    <tr>
    <td align="right">LName:</td>
    <td><asp:TextBox ID="tbLName" runat="server" ></asp:TextBox></td>
    </tr>
    <tr>
    <td align="right">Age:</td><td><asp:TextBox ID="tbAge" runat="server" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td align="right">Gender:</td><td><asp:TextBox ID="tbGender" runat="server" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td></td>
    <td>
    <asp:Button ID="btnInsertPat" Text="AddPatient" runat="server"  OnClick="btnInsertPat_Click"/>
     </td>
     </tr>
    </table>

In InsertNewNode.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class XML_InsertNewPatient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnInsertPat_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Patients.xml"));
        //Load method will internall convert xml document into DOM Tree.
        XmlElement nPatient = doc.CreateElement("Patient");
        XmlElement eVistNum = doc.CreateElement("VistorNum");
        //nPatient.
        //doc.CreateElement(
        eVistNum.InnerText = tbVstrNum.Text;
        XmlElement eFName = doc.CreateElement("FName");
        eFName.InnerText = tbFName.Text;
        XmlElement eLName = doc.CreateElement("LName");
        eLName.InnerText = tbLName.Text;
        XmlElement eAge = doc.CreateElement("Age");
        eAge.InnerText = tbAge.Text;
        XmlElement eGender = doc.CreateElement("gender");
        eGender.InnerText = tbGender.Text;
        nPatient.AppendChild(eVistNum);
        nPatient.AppendChild(eFName);
        nPatient.AppendChild(eLName);
        nPatient.AppendChild(eAge);
        nPatient.AppendChild(eGender);
        //Get the Root Patients Node reference.
        XmlNode root = doc.SelectSingleNode("Patients");
        //Add New patient node to root patient node.
        root.AppendChild(nPatient);
        string sPath = Server.MapPath("Patients.xml");
        doc.Save(sPath);
        //Save method will send changes from XML Dom tree to xml file.
    }
}

Xml file structure:

Create an Xml file with the name Patients.xml as shown below.
<?xml version="1.0" encoding="utf-8"?>
<Patients>
  <Patient type="IP">
    <VistorNum>134</VistorNum>
    <FName>Maha</FName>
    <LName>Lakshmi</LName>
    <Age>89</Age>
    <Gender>FeMale</Gender>
  </Patient>
  <Patient type="OP">
    <VistorNum>135</VistorNum>
    <FName>Kasturi</FName>
    <LName>Rangaiah</LName>
    <Age>88</Age>
    <Gender>Male</Gender>
  </Patient>
  <Patient>
    <VistorNum>138</VistorNum>
    <FName>Rajeev</FName>
    <LName>Ranjan</LName>
    <Age>98</Age>
    <Gender>Male</Gender>
  </Patient>
</Patients>