|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
这个源码是我根据网上一个vb.net编辑xml文件的原理用c#重写的。除重用xml文件外. Ab6Linux联盟 并未重用任何代码!. Ab6Linux联盟 Ab6Linux联盟 这小段代码,可对xml文件的记录进行删除,修改,或增加新记录。 Ab6Linux联盟 利用了datagrid控件的sortcommand事件对xml里的记录进行排序。 Ab6Linux联盟 Ab6Linux联盟 email:ouyang76.263.net Ab6Linux联盟 ------------------------------------------ Ab6Linux联盟 <%@page language="c#" Trace="true"%> Ab6Linux联盟 <%@import namespace="System.Data"%> Ab6Linux联盟 <%@import namespace="System.IO"%> Ab6Linux联盟 <script language="c#" runat="server"> Ab6Linux联盟 string xmlfile="books2.xml",xpath; Ab6Linux联盟 void page_load(Object obj,EventArgs e) Ab6Linux联盟 { Ab6Linux联盟 xpath=Server.MapPath(xmlfile); Ab6Linux联盟 if(!Page.IsPostBack) Ab6Linux联盟 { Ab6Linux联盟 Dataload("isbn"); Ab6Linux联盟 } Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void Dataload(string psort) Ab6Linux联盟 { Ab6Linux联盟 DataSet ds=new DataSet(); Ab6Linux联盟 FileStream fs=new FileStream(xpath,FileMode.Open); Ab6Linux联盟 ds.ReadXml(fs); Ab6Linux联盟 Ab6Linux联盟 if(ds.Tables.Count==0) Ab6Linux联盟 { Ab6Linux联盟 Response.Write("xml文件内无记录!!!!"); Ab6Linux联盟 fs.Close(); Ab6Linux联盟 Response.End(); Ab6Linux联盟 } Ab6Linux联盟 Trace.Warn("表记录数",Convert.ToString(ds.Tables[0].Rows.Count)); Ab6Linux联盟 Ab6Linux联盟 DataRow dr=ds.Tables[0].NewRow();//新建一行 Ab6Linux联盟 dr["ISBN"] = " Add ISBN"; Ab6Linux联盟 ds.Tables[0].Rows.InsertAt(dr,0);//插入到第0行位置 Ab6Linux联盟 Ab6Linux联盟 Trace.Warn("表数目",Convert.ToString(ds.Tables.Count));//以红字显示调试信息 Ab6Linux联盟 Ab6Linux联盟 //grid1.DataSource=ds.Tables[0].DefaultView; Ab6Linux联盟 //grid1.DataBind(); Ab6Linux联盟 Ab6Linux联盟 DataView dv=new DataView(ds.Tables[0]); Ab6Linux联盟 Trace.Warn("字串长度:"+psort,Convert.ToString(psort.Length));//排序字符串的长度 Ab6Linux联盟 if(psort.Length>0) Ab6Linux联盟 dv.Sort=psort; Ab6Linux联盟 Ab6Linux联盟 grid1.DataSource=dv; Ab6Linux联盟 grid1.DataBind(); Ab6Linux联盟 fs.Close(); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void grid_sort(Object obj,DataGridSortCommandEventArgs e) Ab6Linux联盟 { Ab6Linux联盟 if(grid1.EditItemIndex==-1) Ab6Linux联盟 Dataload(e.SortExpression); Ab6Linux联盟 else Ab6Linux联盟 Response.Write("正在编辑暂不能排序!!"); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void grid_edit(Object obj,DataGridCommandEventArgs e) Ab6Linux联盟 { Ab6Linux联盟 grid1.EditItemIndex=(int)e.Item.ItemIndex; Ab6Linux联盟 show_del("hide"); Ab6Linux联盟 Dataload(""); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void grid_cancel(Object obj,DataGridCommandEventArgs e) Ab6Linux联盟 { Ab6Linux联盟 grid1.EditItemIndex=-1; Ab6Linux联盟 show_del("show"); Ab6Linux联盟 Dataload(""); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void grid_update(Object obj,DataGridCommandEventArgs e) Ab6Linux联盟 { Ab6Linux联盟 int numcell=e.Item.Cells.Count;//单元格数目(e.Item是当前发生事件的表格行) Ab6Linux联盟 int currentrow=e.Item.DataSetIndex; Ab6Linux联盟 //int curr2=e.Item.ItemIndex;//与上句等价,可以不带(int) Ab6Linux联盟 Trace.Warn("当前更新行号 = ",Convert.ToString(currentrow)); Ab6Linux联盟 //Trace.Warn("2当前更新行号 = ",Convert.ToString(curr2)); Ab6Linux联盟 Ab6Linux联盟 DataSet ds=new DataSet(); Ab6Linux联盟 ds.ReadXml(xpath);//将xml模式和数据读取到dataSet; Ab6Linux联盟 DataRow dr;//表示DataTable中的一行信息. Ab6Linux联盟 Ab6Linux联盟 if(currentrow==0) Ab6Linux联盟 dr=ds.Tables[0].NewRow(); Ab6Linux联盟 else Ab6Linux联盟 dr=ds.Tables[0].Rows[e.Item.DataSetIndex - 1]; Ab6Linux联盟 Ab6Linux联盟 string[] str={"isbn", "author", "title", "category", "comments"}; Ab6Linux联盟 int j=-1; Ab6Linux联盟 for(int i=2;i<numcell;i++)//跳过1和2column Ab6Linux联盟 { Ab6Linux联盟 j=j+1; Ab6Linux联盟 string ctext; Ab6Linux联盟 ctext=((TextBox)e.Item.Cells[i].Controls[0]).Text; Ab6Linux联盟 Ab6Linux联盟 dr[str[j]] = ctext; Ab6Linux联盟 Trace.Warn(Convert.ToString(i)+str[j]+":每一行的文本",ctext); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 if(currentrow==0) Ab6Linux联盟 { Ab6Linux联盟 Response.Write("加入新记录!!"); Ab6Linux联盟 ds.Tables[0].Rows.InsertAt(dr,0); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 ds.WriteXml(xpath);//将表示dataset的xml写入到xml文件中,包括数据和模式. Ab6Linux联盟 grid1.EditItemIndex = -1;//无此句仍在编辑界面 Ab6Linux联盟 show_del("show"); Ab6Linux联盟 Dataload(""); Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void show_del(string state) Ab6Linux联盟 { Ab6Linux联盟 string tmp=state; Ab6Linux联盟 switch(tmp) Ab6Linux联盟 { Ab6Linux联盟 case "show": Ab6Linux联盟 grid1.Columns[0].Visible = true; Ab6Linux联盟 break; Ab6Linux联盟 case "hide": Ab6Linux联盟 grid1.Columns[0].Visible = false; Ab6Linux联盟 break; Ab6Linux联盟 default: Ab6Linux联盟 grid1.Columns[0].Visible = true; Ab6Linux联盟 break;//也要带break Ab6Linux联盟 } Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void initialize(Object obj,DataGridItemEventArgs e)//注意参数与其它函数不同 Ab6Linux联盟 { Ab6Linux联盟 //e.Item.Cells[0].Text="aaaaa";// Ab6Linux联盟 if(e.Item.ItemIndex==0)//如果是第一行 Ab6Linux联盟 { Ab6Linux联盟 LinkButton a0=new LinkButton(); Ab6Linux联盟 a0=(LinkButton)e.Item.Cells[0].Controls[0]; Ab6Linux联盟 Ab6Linux联盟 LinkButton a1=new LinkButton(); Ab6Linux联盟 a1=(LinkButton)e.Item.Cells[1].Controls[0];//在grid内建一个linkbutton控件 Ab6Linux联盟 Ab6Linux联盟 if(a0.Text=="删 除") Ab6Linux联盟 a0.Text=""; Ab6Linux联盟 if(a1.Text=="编 辑") Ab6Linux联盟 a1.Text="[AddNew]"; Ab6Linux联盟 } Ab6Linux联盟 } Ab6Linux联盟 Ab6Linux联盟 void grid_del(Object obj,DataGridCommandEventArgs e) Ab6Linux联盟 { Ab6Linux联盟 Response.Write("XX"); Ab6Linux联盟 Trace.Warn("正要删除",Convert.ToString(e.Item.ItemIndex));//控件中的行数 Ab6Linux联盟 int curr=e.Item.ItemIndex; Ab6Linux联盟 DataSet ds=new DataSet(); Ab6Linux联盟 ds.ReadXml(xpath); Ab6Linux联盟 Ab6Linux联盟 DataRow dr=ds.Tables[0].Rows[curr-1];//有一行是新加的。 Ab6Linux联盟 dr.Delete();//找到相应的数据行,将其删除 Ab6Linux联盟 ds.WriteXml(xpath); Ab6Linux联盟 Ab6Linux联盟 grid1.EditItemIndex = -1; Ab6Linux联盟 Dataload(""); Ab6Linux联盟 Ab6Linux联盟 } Ab6Linux联盟 </script> Ab6Linux联盟 Ab6Linux联盟 <form runat="server"> Ab6Linux联盟 <asp:datagrid id="grid1" runat="server" Ab6Linux联盟 alternatingitemstyle-backcolor="#eeeeee" Ab6Linux联盟 headerstyle-backcolor="lightyellow" Ab6Linux联盟 font-size="10pt" Ab6Linux联盟 allowsorting="true" Ab6Linux联盟 onsortcommand="grid_sort" Ab6Linux联盟 oneditcommand="grid_edit" Ab6Linux联盟 oncancelcommand="grid_cancel" Ab6Linux联盟 onupdatecommand="grid_update" Ab6Linux联盟 onitemcreated="initialize" Ab6Linux联盟 ondeletecommand="grid_del" Ab6Linux联盟 bordercolor="#999999" Ab6Linux联盟 > Ab6Linux联盟 <columns> Ab6Linux联盟 <asp:buttoncolumn text="删 除" commandname="delete"/> Ab6Linux联盟 <asp:editcommandcolumn buttontype="linkbutton" updatetext="更 新" canceltext="取 消" edittext="编 辑" headertext=""/> Ab6Linux联盟 </columns> Ab6Linux联盟 </asp:datagrid> Ab6Linux联盟 </form> Ab6Linux联盟 -------------------------------------------------------------------- Ab6Linux联盟 xml文件(文件名:books2.xml) Ab6Linux联盟 Ab6Linux联盟 <?xml version="1.0" standalone="yes"?> Ab6Linux联盟 <books> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>2e2e2we2we2</isbn> Ab6Linux联盟 <author>fefdw</author> Ab6Linux联盟 <title>2e2eef</title> Ab6Linux联盟 <category>324tg</category> Ab6Linux联盟 <comments>r3rrgeqw21</comments> Ab6Linux联盟 </book> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>1234345</isbn> Ab6Linux联盟 <author>ssdfdfe</author> Ab6Linux联盟 <title>fgregre</title> Ab6Linux联盟 <category>r4er43trt</category> Ab6Linux联盟 <comments>r3r3redqeq</comments> Ab6Linux联盟 </book> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>1234345</isbn> Ab6Linux联盟 <author>ssdfdfe</author> Ab6Linux联盟 <title>fgregre</title> Ab6Linux联盟 <category>r4er43trt</category> Ab6Linux联盟 <comments>r3r3redqeq</comments> Ab6Linux联盟 </book> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>0679757651</isbn> Ab6Linux联盟 <author>Tom Peters</author> Ab6Linux联盟 <title>Circle of Innovation</title> Ab6Linux联盟 <category>marketing</category> Ab6Linux联盟 <comments>His most recent book is his best by far!</comments> Ab6Linux联盟 </book> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>0884270610</isbn> Ab6Linux联盟 <author>Eli Goldthrait</author> Ab6Linux联盟 <title>The Goal</title> Ab6Linux联盟 <category>management</category> Ab6Linux联盟 <comments>Advocate of Theory of Constraints as applied to managment and optimization.</comments> Ab6Linux联盟 </book> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>068485600X</isbn> Ab6Linux联盟 <author>Jeff Cox, Howard Stevens</author> Ab6Linux联盟 <title>Selling the Wheel</title> Ab6Linux联盟 <category>management</category> Ab6Linux联盟 <comments>Excellent Treatise/Novel on the entire Sales Cycle</comments> Ab6Linux联盟 </book> Ab6Linux联盟 <book> Ab6Linux联盟 <isbn>0672316498</isbn> Ab6Linux联盟 <author>Alan Cooper</author> Ab6Linux联盟 <title>The Inmates Are Running The Asylum</title> Ab6Linux联盟 <category>management</category> Ab6Linux联盟 <comments>The father of Visual Basic and creator of the new art of Interaction Design - very valuable in designing websites. Basically the worlds most cutting edge thinker in User Interface design aimed at simplifying software use.</comments> Ab6Linux联盟 </book> Ab6Linux联盟 </books>
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|