使用c#+(datagrid控件)编辑xml文件
作者Blog:http://blog.csdn.net/ouyang76cn/
使用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文件相关推荐
- datagrid控件使用技巧大集合
引用自:http://www.cnblogs.com/iCeSnaker/archive/2004/07/31/29017.aspx 1.DataGrid的正反双向排序 http://dev.csdn ...
- DataGrid 控件使用技巧集合
datagrid控件使用技巧大集合 引用自:http://www.cnblogs.com/iCeSnaker/archive/2004/07/31/29017.aspx 1.DataGrid的正反双向 ...
- 使用 ASP+ DataGrid 控件来创建主视图/详细资料视图
Nikhil Kothari Microsoft Corporation 2000年8月 简介 Microsoft® Visual Studio.NET 的下一发行版包括 DataGrid Web 控 ...
- EasyUI datagrid控件的基本使用
首先运行一个它的datagrid示例: 出现下图错误: 这是因为它的示例把json数据放到一个单独.json文件,然后加载,浏览器默认不允许加载本地文件: 下面来看一下此控件的基本使用: 首先做一个基 ...
- C# WPF DataGrid控件的详细介绍和推荐一些样式设计
前面介绍过使用DataGrid简单绑定一个数据模型,接着介绍DataGrid的一些详细操作. 参考:C# WPF DataGrid的使用 定制DataGrid控件基本外观属性 RowBackgroun ...
- java datagrid控件_12款Javascript表格控件(DataGrid)
(图片与项目有出入) 表格控件(DataGrid )允许最终用户阅读和写入到绝大多数数据库的应用程序.DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码.当在设计时设置了DataG ...
- vb的datagrid控件的使用(一)
vb的datagrid控件的使用(一) 时间:2007-05-05 使用 DataGrid 控件 DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset ...
- asp.net中显示DataGrid控件列序号的几种方法
在aps.net中多数据绑定的控件很多,论功能来说,应该属DataGrid最为齐全,但它没有提供现成的显示记录序号的功能,不过我们可以通过它所带的一些参数来间接得到序号,下面来看看怎样得到和显示序号值 ...
- C#中DataGrid控件的基本使用
datagrid控件的作用简单点说就是将表格显示出来,用的多的是直接链接数据库文件,也可以自己见一个表格对象,链接到datagrid控件,下面是一个简单的例子 1 //xinjiabiao 2 Dat ...
最新文章
- InfluxData【环境搭建 01】时序数据库 InfluxDB 最新版本安装启动验证(在线安装+离线安装及各版本下载地址)
- 将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus Storefront
- 字符串类中的StringBuffer,StringBuilder
- 桌面SVN检出这一选项消失
- 开源PHP多应用授权系统源码
- 创业期的软件开发管理(二)
- 618 前夕,不谈促销,京东云带你聊聊技术……
- Anaconda+django写出第一个web app(十一)
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
- 影视后期PR效果窗口
- Keepalived双主热备,实现nginx集群
- java axis_Java 使用Axis实现WebService实例
- CISP 考试教材《第 2 章 知识域:网络安全监管》知识整理
- wps html编辑表格,WPS教你如何使用平板电脑创建和编辑表格批注
- vue运行之神奇的npm install --legacy-peer-deps
- (wa待对拍)HDU - 2057 十六进制加法 难度:C++入门 复杂度:有点复杂 翻译难度:简单...
- 百度推广创意怎么写?百度推广创意十大秘籍
- devops1--k8s安装
- nyoj 一笔画问题
- 数据分析系统的设计与实现
热门文章
- mysql shharding_mysql 技术内幕 的示例数据库
- 5g信号云端服务器,随着5G万物互联的到来,云服务器迎来新机遇
- SIFT和SURF的替换算法——ORB (Oriented FAST and Rotated BRIEF 快速定向和旋转)
- ubuntu系统怎么安装ssh服务器配置,如何在Ubuntu 20.04 LTS上安装SSH服务器
- 用c语言设计一个菜单界面_用这7个方法设计菜单,让餐厅的利润暴涨
- 《概率机器人》速度运动模型gmapping中代码解析
- 在CentOS 6.3 64bit上安装Apache Trafficserver 4.2.3挂载SAS硬盘和SSD
- vim复制代码包含注释时格式会乱掉的解决办法
- iso镜像文件烧写到U盘
- java-错误log4j:WARN No appenders could be found for logger