熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

使用c#(datagrid控件)編輯xml文件

2022-06-13   來源: .NET編程 

  對xml文件的記錄進行刪除修改或增加新記錄
    利用了datagrid控件的sortcommand事件對xml裡的記錄進行排序

  email:
   
    <%@page language=c# Trace=true%>
    <%@import namespace=SystemData%>
    <%@import namespace=SystemIO%>
    <script language=c# runat=server>
    string xmlfile=booksxmlxpath;
    void page_load(Object objEventArgs e)
    {
    xpath=ServerMapPath(xmlfile);
      if(!PageIsPostBack)
       {
          Dataload(isbn);
          }
      }
      void Dataload(string psort)
      {
        DataSet ds=new DataSet();
        FileStream fs=new FileStream(xpathFileModeOpen);
        dsReadXml(fs);
       if(dsTablesCount==)
          {
            ResponseWrite(xml文件內無記錄!!!!);
            fsClose();
            ResponseEnd();
            }
        TraceWarn(表記錄數ConvertToString(dsTables[]RowsCount));
        DataRow dr=dsTables[]NewRow();//新建一行
        dr[ISBN] = Add ISBN;
        dsTables[]RowsInsertAt(dr);//插入到第行位置
        TraceWarn(表數目ConvertToString(dsTablesCount));//以紅字顯示調試信息
        //gridDataSource=dsTables[]DefaultView;
        //gridDataBind();
        DataView dv=new DataView(dsTables[]);
        TraceWarn(字串長度:+psortConvertToString(psortLength));//排序字符串的長度
        if(psortLength>)
             dvSort=psort;
        gridDataSource=dv;
        gridDataBind();
        fsClose();
      }
      void grid_sort(Object objDataGridSortCommandEventArgs e)
      {
       if(gridEditItemIndex==)
         Dataload(eSortExpression);
        else
         ResponseWrite(正在編輯暫不能排序!!);
      }
      void grid_edit(Object objDataGridCommandEventArgs e)
      {
      gridEditItemIndex=(int)eItemItemIndex;
      show_del(hide);
      Dataload();
      }
      void grid_cancel(Object objDataGridCommandEventArgs e)
      {
      gridEditItemIndex=;
      show_del(show);
      Dataload();
      }
      void grid_update(Object objDataGridCommandEventArgs e)
      {
      int numcell=eItemCellsCount;//單元格數目(eItem是當前發生事件的表格行)
      int currentrow=eItemDataSetIndex;
      //int curr=eItemItemIndex;//與上句等價可以不帶(int)
      TraceWarn(當前更新行號 = ConvertToString(currentrow));
      //TraceWarn(當前更新行號 = ConvertToString(curr));
        DataSet ds=new DataSet();
        dsReadXml(xpath);//將xml模式和數據讀取到dataSet;
        DataRow dr;//表示DataTable中的一行信息
         if(currentrow==)
           dr=dsTables[]NewRow();
         else
           dr=dsTables[]Rows[eItemDataSetIndex ];
        string[] str={isbn author title category comments};
        int j=;
        for(int i=;i<numcell;i++)//跳過column
         {
            j=j+;
            string ctext;
            ctext=((TextBox)eItemCells[i]Controls[])Text;
            dr[str[j]] = ctext;
            TraceWarn(ConvertToString(i)+str[j]+:每一行的文本ctext);
           }
        if(currentrow==)
        {
          ResponseWrite(加入新記錄!!);
          dsTables[]RowsInsertAt(dr);
          }
        dsWriteXml(xpath);//將表示dataset的xml寫入到xml文件中包括數據和模式
        gridEditItemIndex = ;//無此句仍在編輯界面
        show_del(show);
        Dataload();
      }
      void show_del(string state)
      {
      string tmp=state;
      switch(tmp)
      {
       case show:
       gridColumns[]Visible = true;
         break;
       case hide:
        gridColumns[]Visible = false;
         break;
         default:
       gridColumns[]Visible = true;
         break;//也要帶break
        }
      }

  void initialize(Object objDataGridItemEventArgs e)//注意參數與其它函數不同
      {
         //eItemCells[]Text=aaaaa;//
         if(eItemItemIndex==)//如果是第一行
         {
         LinkButton a=new LinkButton();
         a=(LinkButton)eItemCells[]Controls[];
         LinkButton a=new LinkButton();
         a=(LinkButton)eItemCells[]Controls[];//在grid內建一個linkbutton控件
        if(aText==刪 除)
            aText=;
        if(aText==編 輯)
             aText=[AddNew];
          }
       }
      void grid_del(Object objDataGridCommandEventArgs e)
      {
      ResponseWrite(XX);
      TraceWarn(正要刪除ConvertToString(eItemItemIndex));//控件中的行數
      int curr=eItemItemIndex;
        DataSet ds=new DataSet();
        dsReadXml(xpath);
        DataRow dr=dsTables[]Rows[curr];//有一行是新加的
        drDelete();//找到相應的數據行將其刪除
        dsWriteXml(xpath);
      gridEditItemIndex = ;
      Dataload();

  }
    </script>

  <form runat=server>
    <asp:datagrid id=grid runat=server
    alternatingitemstylebackcolor=#eeeeee
    headerstylebackcolor=lightyellow
    fontsize=pt
    allowsorting=true
    onsortcommand=grid_sort
    oneditcommand=grid_edit
    oncancelcommand=grid_cancel
    onupdatecommand=grid_update
    onitemcreated=initialize
    ondeletecommand=grid_del
    bordercolor=#
    >
    <columns>
    <asp:buttoncolumn text=刪 除 commandname=delete/>
    <asp:editcommandcolumn buttontype=linkbutton updatetext=更 新 canceltext=取 消 edittext=編 輯 headertext=/>
    </columns>
    </asp:datagrid>
    </form>
   
    xml文件(文件名:booksxml)

  <?xml version= standalone=yes?>
    <books>
      <book>
        <isbn>eewewe</isbn>
        <author>fefdw</author>
        <title>eeef</title>
        <category>tg</category>
        <comments>rrrgeqw</comments>
      </book>
      <book>
        <isbn></isbn>
        <author>ssdfdfe</author>
        <title>fgregre</title>
        <category>rertrt</category>
        <comments>rrredqeq</comments>
      </book>
      <book>
        <isbn></isbn>
        <author>ssdfdfe</author>
        <title>fgregre</title>
        <category>rertrt</category>
        <comments>rrredqeq</comments>
      </book>
      <book>
        <isbn></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></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>X</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></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>


From:http://tw.wingwit.com/Article/program/net/201311/11966.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.