Hey pujandoshi,
Please refer below sample.
Namespaces
C#
using System.Xml;
VB.Net
Imports System.Xml
Code
C#
private void Insert(object sender, EventArgs e)
{
string path = @"C:\Users\anand\Desktop\File\Customers.xml";
XmlDocument doc = new XmlDocument();
if (!System.IO.File.Exists(path))
{
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XmlComment comment = doc.CreateComment("This is an XML Generated File");
XmlElement root = doc.CreateElement("Customers");
XmlElement person = doc.CreateElement("Person");
XmlAttribute Name = doc.CreateAttribute("Name");
XmlElement id = doc.CreateElement("id");
XmlElement country = doc.CreateElement("Country");
Name.Value = txtName.Text;
id.InnerText = txtId.Text;
country.InnerText = txtCountry.Text;
doc.AppendChild(declaration);
doc.AppendChild(comment);
doc.AppendChild(root);
root.AppendChild(person);
person.Attributes.Append(Name);
person.AppendChild(id);
person.AppendChild(country);
doc.Save(path);
}
else
{
doc.Load(path);
XmlElement root = doc.DocumentElement;
XmlElement person = doc.CreateElement("Person");
XmlAttribute name = doc.CreateAttribute("Name");
XmlElement id = doc.CreateElement("id");
XmlElement country = doc.CreateElement("Country");
name.Value = txtName.Text;
id.InnerText = txtId.Text;
country.InnerText = txtCountry.Text;
person.Attributes.Append(name);
person.AppendChild(id);
person.AppendChild(country);
root.AppendChild(person);
doc.Save(path);
}
txtName.Text = String.Empty;
txtId.Text = String.Empty;
txtCountry.Text = String.Empty;
}
VB.Net
Private Sub Insert(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim path As String = "C:\Users\anand\Desktop\File\Customers.xml"
Dim doc As XmlDocument = New XmlDocument()
If Not System.IO.File.Exists(path) Then
Dim declaration As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes")
Dim comment As XmlComment = doc.CreateComment("This is an XML Generated File")
Dim root As XmlElement = doc.CreateElement("Customers")
Dim person As XmlElement = doc.CreateElement("Person")
Dim Name As XmlAttribute = doc.CreateAttribute("Name")
Dim id As XmlElement = doc.CreateElement("id")
Dim country As XmlElement = doc.CreateElement("Country")
Name.Value = txtName.Text
id.InnerText = txtId.Text
country.InnerText = txtCountry.Text
doc.AppendChild(declaration)
doc.AppendChild(comment)
doc.AppendChild(root)
root.AppendChild(person)
person.Attributes.Append(Name)
person.AppendChild(id)
person.AppendChild(country)
doc.Save(path)
Else
doc.Load(path)
Dim root As XmlElement = doc.DocumentElement
Dim person As XmlElement = doc.CreateElement("Person")
Dim name As XmlAttribute = doc.CreateAttribute("Name")
Dim id As XmlElement = doc.CreateElement("id")
Dim country As XmlElement = doc.CreateElement("Country")
name.Value = txtName.Text
id.InnerText = txtId.Text
country.InnerText = txtCountry.Text
person.Attributes.Append(name)
person.AppendChild(id)
person.AppendChild(country)
root.AppendChild(person)
doc.Save(path)
End If
txtName.Text = String.Empty
txtId.Text = String.Empty
txtCountry.Text = String.Empty
End Sub
Screenshots

Inserted Data in XML
