本文实例讲述了c#实现xml文件的读取与写入方法。分享给大家供大家参考。具体如下:
//datatable dateset 都可以用来读取xml数据和写入xml数据 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"]= "技术部"; dt.rows.add(dr); datasetds2 = new dataset(); ds2.tables.add(dt); datasetds = new dataset(); ds.readxml(server.mappath("employee.xml")); ds.merge(ds2);//合并数据 ds.writexml(server.mappath("employee.xml")); page.clientscript.registerstartupscript(gettype(), "", "alert('写入成功')",true); } protected voidbutton2_click(object sender, eventargs e) { datasetds = new dataset(); ds.readxml(server.mappath("employee.xml")); gridview1.datasource = ds; gridview1.databind(); }
希望本文所述对大家的c#程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论