articles

Home / DeveloperSection / Articles / How to read data from XML file in C Sharp

How to read data from XML file in C Sharp

Anonymous User27313 02-Aug-2010

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

Example
ds = newDataSet();
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 set
privatevoid 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

              

        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();
        }
 
        privatevoid 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

 

        privatevoid 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

 

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



Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By