C#实现xml文件的读取与写入简单实例
本文实例讲述了C#实现xml文件的读取与写入方法。分享给大家供大家参考。具体如下:
//DataTableDateSet都可以用来读取xml数据和写入xml数据 protectedvoidButton1_Click(objectsender,EventArgse) { DataTabledt=newDataTable("Employee"); DataColumndcID=newDataColumn("ID",typeof(string)); DataColumndcName=newDataColumn("NAME",typeof(string)); DataColumndcAGE=newDataColumn("AGE",typeof(string)); DataColumndcDEPT=newDataColumn("DEPT",typeof(string)); dt.Columns.AddRange(newDataColumn[]{dcID,dcName,dcAGE,dcDEPT}); DataRowdr=dt.NewRow(); dr["ID"]="3"; dr["NAME"]="chen"; dr["AGE"]="21"; dr["DEPT"]="技术部"; dt.Rows.Add(dr); DataSetds2=newDataSet(); ds2.Tables.Add(dt); DataSetds=newDataSet(); ds.ReadXml(Server.MapPath("Employee.xml")); ds.Merge(ds2);//合并数据 ds.WriteXml(Server.MapPath("Employee.xml")); Page.ClientScript.RegisterStartupScript(GetType(),"","alert('写入成功')",true); } protectedvoidButton2_Click(objectsender,EventArgse) { DataSetds=newDataSet(); ds.ReadXml(Server.MapPath("Employee.xml")); GridView1.DataSource=ds; GridView1.DataBind(); }
希望本文所述对大家的C#程序设计有所帮助。