DataSet读写xml文件
用DataSet和GridView控件来读写xml非常方便,而且有会有直观的显示效果:
读取时:使用ReadXml()方法
写入时:使用WriteXml()方法
我的代码如下:
#region //运用DataSet来读数据mydatasetxml()--ReadXml
public void mydatasetReadxml()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("mytest.xml"));
GridView1.DataSource=ds;
GridView1.DataBind();
}
#endregion
#region //运用DataSet来写数据myDatasetWritexml()--WriteXml
public void myDatasetWritexml()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr[0]="beijing";
dt.Rows.Add(dr);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(Server.MapPath("mytest.xml"));
}
# endregion
//以下是两个按钮实现操作:
protected void btnWrite_Click(object sender, EventArgs e)
{
myDatasetWritexml();
}
protected void btnRead_Click(object sender, EventArgs e)
{
mydatasetReadxml();
}