Home > DeveloperSection > Articles > Reading and Writing XML in C#

Reading and Writing XML in C#


C# C# 
Ratings:
0 Comment(s)
 3091  View(s)
Rate this:

Reading and Writing XML in C#

Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards.

Here I have described how to read and write data in XML file using C#.

For reading and writing data in XML file we used here DataTable. With the help of DataTable we insert new record, updating and searching XML file’s data.

Read Data from XML File

DataTable dt = new DataTable("info");  

 

dt.ReadXmlSchema(Server.MapPath("XMLFile.xml"));

                dt.ReadXml(Server.MapPath("XMLFile.xml"));

Insert Data into XML File

Case 1:- If no XML file has in project then first insert data in DataTable then ‘WriteXml’ properties created a XML file in project and insert data.

DataTable dt = new DataTable("info");

 

//adding up 6 columns to datatable

dt.Columns.Add("id", typeof(int));

dt.Columns.Add("Name", typeof(string));

dt.Columns.Add("Age", typeof(int));

dt.Columns.Add("Salary", typeof(double));

dt.Columns.Add("Country", typeof(string));

dt.Columns.Add("City", typeof(string));

 

//add a row in DataTable and insert data from TextBox

dt.Rows.Add(txtId.Text.Trim(), txtName.Text.Trim(), txtAge.Text.Trim(), txtSalary.Text.Trim(), txtCountry.Text.Trim(), txtCity.Text.Trim());

dt.AcceptChanges();

 

//Write DataTable Data into XML file(XML file are created in project before writing data)

dt.WriteXml(Server.MapPath("XMLFile.xml"));

 

Case 2:-If XML File exist then DataTable read Schema of existing XML file and then insert data into XML file with the help of DataTable

DataTable dt = new DataTable("info");  

 

if (File.Exists(Server.MapPath("XMLFile.xml")))

 {

    dt.ReadXmlSchema(Server.MapPath("XMLFile.xml"));

    dt.ReadXml(Server.MapPath("XMLFile.xml"));

    dt.Rows.Add(txtId.Text.Trim(), txtName.Text.Trim(), txtAge.Text.Trim(),               txtSalary.Text.Trim(), txtCountry.Text.Trim(), txtCity.Text.Trim());

    dt.AcceptChanges();

    dt.WriteXml(Server.MapPath("XMLFile.xml"));

 }

Searching Data in XML file

DataTable dt = new DataTable("info");  

 

if (txtId.Text != null)

            {

                dt.ReadXmlSchema(Server.MapPath("XMLFile.xml"));

                dt.ReadXml(Server.MapPath("XMLFile.xml"));

                DataRow[] data = dt.Select("Id='" + txtId.Text + "'");

                if (data.Length != 0)

                {

                    txtName.Text = data[0][1].ToString();

                    txtAge.Text = data[0][2].ToString();

                    txtSalary.Text = data[0][3].ToString();

                    txtCountry.Text = data[0][4].ToString();

                    txtCity.Text = data[0][5].ToString();

                    btnUpdate.Enabled = true;

                    btnInsert.Enabled = false;

                }

                else

                {

                    txtName.Text = txtAge.Text = txtSalary.Text = txtCountry.Text = txtCity.Text = "";

                    txtName.Focus();

                    btnUpdate.Enabled = false;

                    btnInsert.Enabled = true;

                }

            }

Updating Data in XML file

DataTable dt = new DataTable("info");  

 

dt.ReadXmlSchema(Server.MapPath("XMLFile.xml"));

            dt.ReadXml(Server.MapPath("XMLFile.xml"));

            DataRow[] data = dt.Select("Id='" + txtId.Text + "'");

            data[0][1] = txtName.Text.Trim();

            data[0][2] = txtAge.Text.Trim();

            data[0][3] = txtSalary.Text.Trim();

            data[0][4] = txtCountry.Text.Trim();

            data[0][5] = txtCity.Text.Trim();

            dt.AcceptChanges();

            dt.WriteXml(Server.MapPath("XMLFile.xml"));

 


Don't want to miss updates? Please click the below button!

Follow MindStick