To read data from XML file we have to assign XML file to data set using ReadXml() function of dataset.

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


              

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


 

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


 

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


  Modified On Nov-20-2017 11:30:28 PM
  1. hi ya,

    Impressive work.Im currently working the XML ..and what I can share the community is to convert XML to Word in C# ,then open it in MS Word.Hope this would be a option for reading xml files.

    thanks and regards.

Leave Comment