Users Pricing

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 User 27947 02 Aug 2010 Updated 04 Mar 2020

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 = 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 set
private 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

              

        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

 

        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

 

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



I am a content writter !


2 Comments