使用c#+(datagrid控件)编辑xml文件
使用c#+(datagrid控件)編輯xml文件
這個源碼是我根據網上一個vb.net編輯xml文件的原理用c#重寫的。除重用xml文件外.
并未重用任何代碼!.
這小段代碼,可對xml文件的記錄進行刪除,修改,或增加新記錄。
利用了datagrid控件的sortcommand事件對xml里的記錄進行排序。
email:ouyang76.263.net
------------------------------------------
<%@page language="c#" Trace="true"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.IO"%>
<script language="c#" runat="server">
string xmlfile="books2.xml",xpath;
void page_load(Object obj,EventArgs e)
{
xpath=Server.MapPath(xmlfile);
? if(!Page.IsPostBack)
?? {
????? Dataload("isbn");
????? }
? }
?
? void Dataload(string psort)
? {
??? DataSet ds=new DataSet();
??? FileStream fs=new FileStream(xpath,FileMode.Open);
??? ds.ReadXml(fs);
??
?? if(ds.Tables.Count==0)
????? {
??????? Response.Write("xml文件內無記錄!!!!");
??????? fs.Close();
??????? Response.End();
??????? }
??? Trace.Warn("表記錄數",Convert.ToString(ds.Tables[0].Rows.Count));
???
??? DataRow dr=ds.Tables[0].NewRow();//新建一行
??? dr["ISBN"] = " Add ISBN";
??? ds.Tables[0].Rows.InsertAt(dr,0);//插入到第0行位置
??
??? Trace.Warn("表數目",Convert.ToString(ds.Tables.Count));//以紅字顯示調試信息
???
??? //grid1.DataSource=ds.Tables[0].DefaultView;
??? //grid1.DataBind();
???
??? DataView dv=new DataView(ds.Tables[0]);
??? Trace.Warn("字串長度:"+psort,Convert.ToString(psort.Length));//排序字符串的長度
??? if(psort.Length>0)
???????? dv.Sort=psort;
???????
??? grid1.DataSource=dv;
??? grid1.DataBind();
??? fs.Close();
? }
?
? void grid_sort(Object obj,DataGridSortCommandEventArgs e)
? {
?? if(grid1.EditItemIndex==-1)
???? Dataload(e.SortExpression);
??? else
???? Response.Write("正在編輯暫不能排序!!");
? }
?
? void grid_edit(Object obj,DataGridCommandEventArgs e)
? {
? grid1.EditItemIndex=(int)e.Item.ItemIndex;
? show_del("hide");
? Dataload("");
? }
?
? void grid_cancel(Object obj,DataGridCommandEventArgs e)
? {
? grid1.EditItemIndex=-1;
? show_del("show");
? Dataload("");
? }
?
? void grid_update(Object obj,DataGridCommandEventArgs e)
? {
? int numcell=e.Item.Cells.Count;//單元格數目(e.Item是當前發生事件的表格行)
? int currentrow=e.Item.DataSetIndex;
? //int curr2=e.Item.ItemIndex;//與上句等價,可以不帶(int)
? Trace.Warn("當前更新行號 = ",Convert.ToString(currentrow));
? //Trace.Warn("2當前更新行號 = ",Convert.ToString(curr2));
?
??? DataSet ds=new DataSet();
??? ds.ReadXml(xpath);//將xml模式和數據讀取到dataSet;
??? DataRow dr;//表示DataTable中的一行信息.
?
???? if(currentrow==0)
?????? dr=ds.Tables[0].NewRow();
???? else
?????? dr=ds.Tables[0].Rows[e.Item.DataSetIndex - 1];
???
??? string[] str={"isbn", "author", "title", "category", "comments"};
??? int j=-1;
??? for(int i=2;i<numcell;i++)//跳過1和2column
???? {
??????? j=j+1;
??????? string ctext;
??????? ctext=((TextBox)e.Item.Cells[i].Controls[0]).Text;
???????
??????? dr[str[j]] = ctext;
??????? Trace.Warn(Convert.ToString(i)+str[j]+":每一行的文本",ctext);
?????? }
??????
??? if(currentrow==0)
??? {
????? Response.Write("加入新記錄!!");
????? ds.Tables[0].Rows.InsertAt(dr,0);
????? }
?????
??? ds.WriteXml(xpath);//將表示dataset的xml寫入到xml文件中,包括數據和模式.
??? grid1.EditItemIndex = -1;//無此句仍在編輯界面
??? show_del("show");
??? Dataload("");
? }
?
? void show_del(string state)
? {
? string tmp=state;
? switch(tmp)
? {
?? case "show":
?? grid1.Columns[0].Visible = true;
???? break;
?? case "hide":
??? grid1.Columns[0].Visible = false;
???? break;
???? default:
?? grid1.Columns[0].Visible = true;
???? break;//也要帶break
??? }
? }
? void initialize(Object obj,DataGridItemEventArgs e)//注意參數與其它函數不同
? {
???? //e.Item.Cells[0].Text="aaaaa";//
???? if(e.Item.ItemIndex==0)//如果是第一行
???? {
???? LinkButton a0=new LinkButton();
???? a0=(LinkButton)e.Item.Cells[0].Controls[0];
????
???? LinkButton a1=new LinkButton();
???? a1=(LinkButton)e.Item.Cells[1].Controls[0];//在grid內建一個linkbutton控件
????
??? if(a0.Text=="刪 除")
??????? a0.Text="";
??? if(a1.Text=="編 輯")
???????? a1.Text="[AddNew]";
????? }
?? }
??
? void grid_del(Object obj,DataGridCommandEventArgs e)
? {
? Response.Write("XX");
? Trace.Warn("正要刪除",Convert.ToString(e.Item.ItemIndex));//控件中的行數
? int curr=e.Item.ItemIndex;
??? DataSet ds=new DataSet();
??? ds.ReadXml(xpath);
???
??? DataRow dr=ds.Tables[0].Rows[curr-1];//有一行是新加的。
??? dr.Delete();//找到相應的數據行,將其刪除
??? ds.WriteXml(xpath);
?
? grid1.EditItemIndex = -1;
? Dataload("");
? }
</script>
<form runat="server">
<asp:datagrid id="grid1" runat="server"
alternatingitemstyle-backcolor="#eeeeee"
headerstyle-backcolor="lightyellow"
font-size="10pt"
allowsorting="true"
onsortcommand="grid_sort"
oneditcommand="grid_edit"
oncancelcommand="grid_cancel"
onupdatecommand="grid_update"
onitemcreated="initialize"???
ondeletecommand="grid_del"
bordercolor="#999999"
>
<columns>
<asp:buttoncolumn text="刪 除" commandname="delete"/>
<asp:editcommandcolumn buttontype="linkbutton" updatetext="更 新" canceltext="取 消" edittext="編 輯" headertext=""/>
</columns>
</asp:datagrid>
</form>
--------------------------------------------------------------------
xml文件(文件名:books2.xml)
<?xml version="1.0" standalone="yes"?>
<books>
? <book>
??? <isbn>2e2e2we2we2</isbn>
??? <author>fefdw</author>
??? <title>2e2eef</title>
??? <category>324tg</category>
??? <comments>r3rrgeqw21</comments>
? </book>
? <book>
??? <isbn>1234345</isbn>
??? <author>ssdfdfe</author>
??? <title>fgregre</title>
??? <category>r4er43trt</category>
??? <comments>r3r3redqeq</comments>
? </book>
? <book>
??? <isbn>1234345</isbn>
??? <author>ssdfdfe</author>
??? <title>fgregre</title>
??? <category>r4er43trt</category>
??? <comments>r3r3redqeq</comments>
? </book>
? <book>
??? <isbn>0679757651</isbn>
??? <author>Tom Peters</author>
??? <title>Circle of Innovation</title>
??? <category>marketing</category>
??? <comments>His most recent book is his best by far!</comments>
? </book>
? <book>
??? <isbn>0884270610</isbn>
??? <author>Eli Goldthrait</author>
??? <title>The Goal</title>
??? <category>management</category>
??? <comments>Advocate of Theory of Constraints as applied to managment and optimization.</comments>
? </book>
? <book>
??? <isbn>068485600X</isbn>
??? <author>Jeff Cox, Howard Stevens</author>
??? <title>Selling the Wheel</title>
??? <category>management</category>
??? <comments>Excellent Treatise/Novel on the entire Sales Cycle</comments>
? </book>
? <book>
??? <isbn>0672316498</isbn>
??? <author>Alan Cooper</author>
??? <title>The Inmates Are Running The Asylum</title>
??? <category>management</category>
??? <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>
? </book>
</books>
總結
以上是生活随笔為你收集整理的使用c#+(datagrid控件)编辑xml文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 把Excel文件中的数据读入到DataG
- 下一篇: 如何创建一个用弹出窗口来查看详细信息的超