To read data from XML file we have to assign XML file to data set using ReadXml() function of dataset.
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;
}
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);
}
}
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);
}
}
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();
}
Anonymous User
20-Mar-2019Very nice article.
deve chirs
24-Jul-2014Impressive 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.