XML常用操作之一

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default:System.Web.UI.Page
... {
protectedvoidPage_Load(objectsender,EventArgse)
...{
}

//读取数据
protectedvoidButton1_Click(objectsender,EventArgse)
...{
DataSetds
=newDataSet();
ds.ReadXml(Server.MapPath(
"book.xml"));
if(ds.Tables.Count>0)
...{
this.GridView1.DataSource=ds;
this.GridView1.DataBind();
}

}

//过滤数据
protectedvoidButton2_Click(objectsender,EventArgse)
...{
DataSetds
=newDataSet();
ds.ReadXmlSchema(Server.MapPath(
"book.xsd"));
ds.ReadXml(Server.MapPath(
"book.xml"));

DataViewdv
=newDataView(ds.Tables[0]);
dv.Sort
="pricedesc";
//dv.RowFilter="title='database设计'";
this.GridView1.DataSource=dv;
this.GridView1.DataBind();
}

//添加记录
protectedvoidButton3_Click(objectsender,EventArgse)
...{
DataSetds
=newDataSet();
ds.ReadXml(Server.MapPath(
"book.xml"));
ds.ReadXmlSchema(Server.MapPath(
"book.xsd"));
DataTabledt
=ds.Tables[0];
DataRowdr
=dt.NewRow();
dr[
0]="1";
dr[
1]="2";
dr[
2]="3";
dt.Rows.Add(dr);
//ds.Tables.Add(dt);
ds.WriteXml(Server.MapPath("book.xml"));
this.GridView1.DataSource=ds;
this.GridView1.DataBind();
}

//更新数据
protectedvoidButton4_Click(objectsender,EventArgse)
...{
DataSetds
=newDataSet();
ds.ReadXml(Server.MapPath(
"book.xml"));
ds.ReadXmlSchema(Server.MapPath(
"book.xsd"));
for(inti=0;i<ds.Tables[0].Rows.Count;i++)
...{
if(ds.Tables[0].Rows[i]["title"].ToString().Trim()=="asp.net")
...{
for(intj=0;j<ds.Tables[0].Columns.Count;j++)
...{
ds.Tables[
0].Rows[i][j]=j;
}

}

}

ds.AcceptChanges();
ds.WriteXml(Server.MapPath(
"book.xml"));
this.GridView1.DataSource=ds;
this.GridView1.DataBind();
}

protectedvoidButton5_Click(objectsender,EventArgse)
...{
this.del(10);
}

//删除记录
privatevoiddel(intstrid)
...{
DataSetds
=newDataSet();
ds.ReadXml(Server.MapPath(
"book.xml"));
ds.ReadXmlSchema(Server.MapPath(
"book.xsd"));
ds.Tables[
0].Rows[strid].Delete();
ds.AcceptChanges();
ds.WriteXml(Server.MapPath(
"book.xml"));
this.GridView1.DataSource=ds;
this.GridView1.DataBind();
}

}

你可能感兴趣的:(Web,xml,UI,asp.net,asp)