01.
private
void
btnCreateXML_Click(
object
sender, EventArgs e)
02.
{
03.
try
04.
{
05.
//Create a datatable to store XML data
06.
DataTable DT =
new
DataTable();
07.
DT.Columns.Add(
"FirstName"
);
08.
DT.Columns.Add(
"LastName"
);
09.
DT.Columns.Add(
"Gender"
);
10.
DT.Columns.Add(
"Age"
);
11.
DT.Rows.Add(
new
object
[] {
"Nick"
,
"Solimine"
,
"Male"
, 22 });
12.
DT.Rows.Add(
new
object
[] {
"Mark"
,
"Taylor"
,
"Male"
, 32 });
13.
DT.Rows.Add(
new
object
[] {
"Alice"
,
"Warden"
,
"Female"
, 20 });
14.
15.
//Create a dataset
16.
DS =
new
DataSet();
17.
18.
//Add datatable to this dataset
19.
DS.Tables.Add(DT);
20.
21.
//Write dataset to XML file
22.
DS.WriteXml(txtXMLFilePath.Text);
23.
24.
MessageBox.Show(
"XML data written successfully to "
+txtXMLFilePath.Text);
25.
}
26.
catch
(Exception ex)
27.
{
28.
MessageBox.Show(
"Exception: "
+ex.Message);
29.
}
30.
}
01.
private
void
btnReadXML_Click(
object
sender, EventArgs e)
02.
{
03.
try
04.
{
05.
//Initialize new Dataset
06.
DS =
new
DataSet();
07.
08.
//Read XML data from file
09.
DS.ReadXml(txtXMLFilePath.Text);
10.
11.
//Fill grid with XML data
12.
dataGridView1.DataSource = DS.Tables[0];
13.
dataGridView1.Refresh();
14.
15.
MessageBox.Show(
"XML data read successful"
);
16.
}
17.
catch
(Exception ex)
18.
{
19.
MessageBox.Show(
"Exception: "
+ ex.Message);
20.
}
21.
}
01.
private
void
btnSaveXML_Click(
object
sender, EventArgs e)
02.
{
03.
try
04.
{
05.
//Write dataset to XML file
06.
DS.WriteXml(txtXMLFilePath.Text);
07.
MessageBox.Show(
"XML data saved successfully to "
+ txtXMLFilePath.Text);
08.
}
09.
catch
(Exception ex)
10.
{
11.
MessageBox.Show(
"Exception: "
+ ex.Message);
12.
}
13.
}
1.
private
void
btnClearGrid_Click(
object
sender, EventArgs e)
2.
{
3.
//Clear Grid by setting it's datasource property to null.
4.
dataGridView1.DataSource =
null
;
5.
}
No comments:
Post a Comment