---
title: "How to read data from XML file in C Sharp"  
description: "/* Style Definitions */   table.MsoNormalTable  	{mso-style-name:\"Table Normal\";  	mso-tstyle-rowband-size:0;  	mso-tstyle-colband-size:0;  	mso-st"  
author: "Anonymous User"  
published: 2010-08-02  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/76/how-to-read-data-from-xml-file-in-c-sharp  
category: "xml"  
tags: ["xml"]  
reading_time: 2 minutes  

---

# How to read data from XML file in C Sharp

To read data from XML file we have to assign XML file to data set using ReadXml() function of dataset.

![How to read data from XML file in C Sharp](https://www.mindstick.com/mindstickarticle/851bfc02-9c84-41c4-a3ca-d83b5fd0adf2/images/4facbbd6-9879-49f1-a471-1cc2bb55223b.png)

##### Example

```
ds = new DataSet();ds.ReadXml("C:\\dataEmployee.xml");//reading XML file and storing its data to dataset.dt = ds.Tables["Employee"]; Code for moving around data of fetched from XML file.int count = 0;//counter to get the current position of data fetched from data setprivate void btnShow_Click(object sender, EventArgs e)        {//displaying first record.            display();                        panel1.Enabled = true;            btnInsert.Enabled = false;        }
```

\

![How to read data from XML file in C Sharp](https://www.mindstick.com/mindstickarticle/851bfc02-9c84-41c4-a3ca-d83b5fd0adf2/images/f05fd6e3-745b-4b48-80c8-48282bc33b90.png)

```
        void display()        {           //this function will display the data in text box when ever it is called.            DataRow dr = dt.Rows[count];            txtId.Text = dr[0].ToString().Trim();            txtName.Text = dr[1].ToString();            txtAddress.Text = dr[2].ToString();            txtId.Focus();        }         private void btnNext_Click(object sender, EventArgs e)        {                      //moving to next data in dataset.              try            {                if (count < dt.Rows.Count - 1)                {                    count++;                    display();                                    }                else                                        MessageBox.Show("Last record", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }
```

\

![How to read data from XML file in C Sharp](https://www.mindstick.com/mindstickarticle/851bfc02-9c84-41c4-a3ca-d83b5fd0adf2/images/43498d2f-6d96-41b3-8a71-33f7070aa151.png)

```
        private void btnPrevious_Click(object sender, EventArgs e)        {                      //moving to previous data in dataset.            try            {                if (count > 0)                {                    count--;                    display();                }                else                    MessageBox.Show("First record", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }
```

\

![How to read data from XML file in C Sharp](https://www.mindstick.com/mindstickarticle/851bfc02-9c84-41c4-a3ca-d83b5fd0adf2/images/8b3e2082-a036-4e80-b782-a6147f1ee627.png)

```
        private void btnFirst_Click(object sender, EventArgs e)        {           //moving to first record in data set            count = 0;            display();        }         private void btnLast_Click(object sender, EventArgs e)        {          //moving to last record in data set.            count = dt.Rows.Count-1;            display();        }
```

\

---

Original Source: https://www.mindstick.com/articles/76/how-to-read-data-from-xml-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
