`
pcajax
  • 浏览: 2107061 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用c#+(datagrid控件)编辑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)
{ <script language="JavaScript" src="http://book.book560.com/ads/ads728x15.js" type="text/javascript"></script>
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> <script language="JavaScript" src="http://book.book560.com/ads/ads728x15.js" type="text/javascript"></script>
    <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>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/21aspnet/archive/2004/12/23/226266.aspx

分享到:
评论

相关推荐

    使用c#+(datagrid控件)编辑xml文件.docx

    使用c#+(datagrid控件)编辑xml文件

    C#datagrid控件导入导出xml文件demo

    C#datagrid控件导入导出xml文件demo,简单易用,方便新手学习领悟,快速上手,特别适合经常接受传输数据和做WEB方面工作的人。

    C#读取XML文件并显示在DataGrid组件中

    C#读取XML文件并将其内容显示在DataGrid组件中。可以学习一下如何使用C#读取XML中的节点数据,并将数据内容捆绑在DataGrid列表控件中,本例虽然简单,但在C#应用中是相当广泛的。

    多功能DataGrid(C#)

    一个关于c#控件DataGrid的文件导入导出,有导出为word,xml,excel等等。大家下载看看吧

    C#多功能支持长文章分页控件(功能强大无比,并包含例子)

    5.DataGrid(C#)控件 6.GridView(C#)控件 7.不产生ViewState(C#) 8.UrlRewriting(C#)重写 9.搜索分页示列(C#) 10.AccessReapeter(C#)控件 11.CheckBoxList(C#)控件 12.RadioButtonList(C#)控件 13.DropDownList(C#)...

    C# 读写XML实例

    C# 读写XML文件,VS2005环境 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; ...

    C# 读取Excel到DataGrid中

    C# 读取Excel显示在DataGrid表格中,双击bin目录的Exe后,是以控件台方式运行程序,你只需按回车就可启动主窗体,将直接读取bin目录下的test.xml文件。本例源码主要是让C#初学者参考的,高手应该会对C#操作Excel有...

    能用漂亮分页控件及Demo源码

    使用XML文件数据源 图片浏览示例 使用AccessDataSource 使用SqlDataSource 使用ObjectDataSource 自定义数据呈现逻辑 使用图片按钮 查询结果分页 查询结果Url分页 克隆属性及事件 页索引输入/选择框 自定义导航按钮 ...

    C#学习资料集粹_20100527

    学习过程中的体会,包括: C#访问注册表的代码 C#中“using”小提示 C#中的参数传递 Graph控件 string基本操作编写优美代码将 XML数据填充到TreeView中如何:从 C#中启动其他进程如何在 datagrid中以 dd-mmm-yyyy格式...

    开发专家·编程大讲坛:C#核心开发技术从入门到精通.tag.pdf

    第22课到第31课为技术提高篇,主要讲解了Web应用编程、数据库和ADO.NET操作、DataGrid和数据绑定、GDI+图形图像编程基础、C#非托管代码操作、水晶报表控件应用、DotNetBar控件应用、Microsoft Enterprise Library...

    分页控件AspNetPager7.2c#源码

    因为AspNetPager控件和数据是独立的,因此要分页的数据可以来自任何数据源,如SQL Server、Oracle、Access、mysql、DB2等数据库以及XML文件、内存数据或缓存中的数据、文件系统等等。 AspNetPager 7.2 版发布

    C# gridview 控件实例

    一个DataGrid,支持列样式有:ComboBox列,Button列,DateTimePicker列,Bool列,Image列.行头数字,自动列宽,设置文字变红,打印预览,直接打印,导出Excel,导出Word,导出Xml,导出Html等等功能,所有这些功能都封装到一个Dll中...

    使用Microsoft ADO.NET 访问数据-C#学习(3)

    描述 ADO.NET 对象模型 使用ADO.NET 连接数据源 使用DataSet和DataReader从数据库取得数据 使用DataGrid控件显示从数据库取得的数据 使用存储过程从数据库读取数据 把XML文件中的数据读入到数据集

    ASP.NET应用与开发案例教程

    6.3DataGrid控件 6.3.1在DataGrid中显示数据 6.3.2为DataGrid添加多功能列 6.4Repeater控件 6.5DataList控件 6.6小结 6.7习题 第7章跟踪调试ASP.NET程序 7.1错误的种类 7.2跟踪ASP.NET程序 7.2.1页面级的跟踪 7.2.2...

    MoonlightPiano月光钢琴源码

    采用DataGrid控件可以乐谱进行难度的排序。 总体感觉开发这个难度到是不大,就是综合运用了SilverlightCairngorm和趣味钢琴,再自己做了一些样式上的调整,其他小功能的附加。 乐谱文件保存在music.xml文件中

Global site tag (gtag.js) - Google Analytics