A simple example of reading and writing xml files by C
- 2021-07-24 11:37:28
- OfStack
In this paper, an example is given to describe the method of reading and writing xml files by C #. Share it for your reference. The details are as follows:
//DataTable DateSet Can be used to read xml Data and writes xml Data
protected voidButton1_Click(object sender, EventArgs e)
{
DataTabledt = new DataTable("Employee");
DataColumndcID = new DataColumn("ID", typeof(string));
DataColumndcName = new DataColumn("NAME", typeof(string));
DataColumndcAGE = new DataColumn("AGE", typeof(string));
DataColumndcDEPT = new DataColumn("DEPT", typeof(string));
dt.Columns.AddRange(new DataColumn[] {dcID, dcName, dcAGE, dcDEPT });
DataRowdr = dt.NewRow();
dr["ID"]= "3";
dr["NAME"]= "chen";
dr["AGE"]= "21";
dr["DEPT"]= " Technical Department ";
dt.Rows.Add(dr);
DataSetds2 = new DataSet();
ds2.Tables.Add(dt);
DataSetds = new DataSet();
ds.ReadXml(Server.MapPath("Employee.xml"));
ds.Merge(ds2);// Merge data
ds.WriteXml(Server.MapPath("Employee.xml"));
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert(' Write successful ')",true);
}
protected voidButton2_Click(object sender, EventArgs e)
{
DataSetds = new DataSet();
ds.ReadXml(Server.MapPath("Employee.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
I hope this article is helpful to everyone's C # programming.