---
title: "Reading and Writing XML in C#"  
description: "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 m"  
author: "AVADHESH PATEL"  
published: 2012-08-01  
updated: 2019-11-24  
canonical: https://www.mindstick.com/articles/971/reading-and-writing-xml-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Reading and Writing XML in C#

[Extensible](https://www.mindstick.com/blog/59/xaml-extensible-application-markup-language) [Markup Language](https://www.mindstick.com/interview/234/what-is-markup-language) (XML) is a markup language that defines a set of rules for encoding [documents](https://www.mindstick.com/articles/156931/the-main-types-of-business-documents) in a format that is both human-readable and machine-readable. It is defined in the XML 1.0 [Specification](https://www.mindstick.com/news/369/specifications-of-redmi-note-10-series-leaked-before-the-official-launch) 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](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) using C#.

For reading and writing data in XML file we used here [DataTable](https://www.mindstick.com/articles/122/datatable). With the help of DataTable we insert new record, updating and searching XML file’s data.

```
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](https://www.mindstick.com/forum/172/how-v-insert-data-into-table-and-display-into-gridview-in-aspx-cs-file-with-out-using-wizard) in DataTable then ‘WriteXml’ [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) created a XML file in project and insert data.

```
DataTable dt = new DataTable("info"); //adding up 6 columns to datatabledt.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 TextBoxdt.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](https://www.mindstick.com/forum/34415/how-to-check-file-exist-or-not-in-mvc-razor-view) 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"));
```

---

Original Source: https://www.mindstick.com/articles/971/reading-and-writing-xml-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
