Repeater、GridView等 操作XML文件

在项目中需要对XML文件进行管理,我用了Repeater 的方式来实现了实时的更新

希望对阅读这篇随笔的您有所帮助,我用的Repeater,您也可以用GridView等...区别仅仅在于控件内的按钮的事件中获取当前操作对象索引的方式..

如果您的XML文档格式与此不同,建议使用下面的XML格式(因为这个格式能够通过DataSet的LoadXML方法直接获得数据源,并且能够使用DateSet的WriteXML方法输出XML文档)或者您可以根基自己的格式重新写ReadXmlInfo和WriteXMLInfo这两个方法。

首先是我的XML格式

代码

<?xml version="1.0" standalone="yes"?>
<imgs>
<pic name="/adv_pic/1.jpg" url="http://www.baidu.com/" title="美国影视中的黑人总统盘点" />
<pic name="/adv_pic/2.jpg" url="http://www.baidu.com/" title="斯琴格日乐--我很好!" />
<pic name="/adv_pic/3.jpg" url="http://www.baidu.com/" title="伦敦金融城市长秀" />
<pic name="/adv_pic/4.jpg" url="http://www.baidu.com/" title="市长大人的金色大马车" />
<pic name="/adv_pic/5.jpg" url="http://www.baidu.com/" title="英国皇家歌剧院艺术家为公益缘何全裸?" />
</imgs>

<?xml version="1.0" standalone="yes"?><imgs>  <pic name="/adv_pic/1.jpg" url="http://www.baidu.com/" title="美国影视中的黑人总统盘点" />  <pic name="/adv_pic/2.jpg" url="http://www.baidu.com/" title="斯琴格日乐--我很好!" />  <pic name="/adv_pic/3.jpg" url="http://www.baidu.com/" title="伦敦金融城市长秀" />  <pic name="/adv_pic/4.jpg" url="http://www.baidu.com/" title="市长大人的金色大马车" />  <pic name="/adv_pic/5.jpg" url="http://www.baidu.com/" title="英国皇家歌剧院艺术家为公益缘何全裸?" /></imgs>

HTML Code:

代码

<%@ Register Assembly="Anthem" Namespace="Anthem" TagPrefix="anthem" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>flash橱窗管理</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href="../css/css.css" type="text/css" rel="stylesheet">
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;">
<anthem:Repeater ID="Repeater1" runat="server" AutoUpdateAfterCallBack="true">
<HeaderTemplate>
<table class="DataList" cellspacing="0" rules="all" border="1" id="Datagrid2" style="width: 100%;
border-collapse: separate;">
<tr class="DataList_Head" align="Left">
<td align="center" style="width: 2%;">
编号</td>
<td align="center" style="width: 30%;">
标题</td>
<td align="center" style="width: 30%;">
地址</td>
<td align="center" style="width: 30%;">
图片</td>
<td align="center" style="width: 8%;">
操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="DataList_Item" align="Left">
<td align="center" style="width: 2%;">
<%#Container.ItemIndex%></td>
<td align="center" style="width: 30%;">
<a href=‘<%# DataBinder.Eval(Container.DataItem,"url")%>‘
target="_blank"><%# DataBinder.Eval(Container.DataItem,"title")%></a>
</td>
<td align="center" style="width: 30%;">
<a href=‘<%# DataBinder.Eval(Container.DataItem,"url")%>‘
target="_blank">
<%#DataBinder.Eval(Container.DataItem, "url").ToString()%></a>
</td>
<td align="center" style="width: 30%;">
<a href=‘<%# DataBinder.Eval(Container.DataItem,"name")%>‘
target="_blank"><%# DataBinder.Eval(Container.DataItem,"name")%></a>
</td>
<td align="center" style="width: 10%;">
<anthem:LinkButton Text="修改" ID="btn_Edit" OnClick="btn_Edit_Click" runat="server" ToolTip=‘<%#Container.ItemIndex%>‘></anthem:LinkButton>
<anthem:LinkButton Text="删除" ID="btn_Delete" OnClick="btn_Delete_Click" PreCallBackFunction="function (){return confirm(‘确定删除?‘);}" runat="server" ToolTip=‘<%#Container.ItemIndex%>‘></anthem:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</anthem:Repeater>
<table width="90%" cellpadding="5" bgcolor="#e7ecce" border="1">
<tr>
<td align="center" colspan="2">
<anthem:Label ID="Txt_TableTitle" runat="server" AutoUpdateAfterCallBack="true" Text="添加新项"></anthem:Label>
<anthem:HiddenField ID="Txt_ItemIndex" runat="server" AutoUpdateAfterCallBack="true" />
</td>
</tr>
<tr>
<td align="right">
标题:&nbsp;
</td>
<td align="left">
<anthem:TextBox ID="Txt_title" runat="server" AutoUpdateAfterCallBack="true" Width="301px"></anthem:TextBox></td>
</tr>
<tr>
<td align="right">
地址:</td>
<td align="left">
<anthem:TextBox ID="Txt_url" runat="server" AutoUpdateAfterCallBack="true" Width="301px"></anthem:TextBox></td>
</tr>
<tr>
<td align="right">
图片地址</td>
<td align="left">
<anthem:TextBox ID="Txt_name" runat="server" Width="303px" AutoUpdateAfterCallBack="true"></anthem:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2">
<anthem:Button ID="Btn_Submit" runat="server" Text=" 确 定 " CssClass="button" OnClick="Btn_Submit_Click" AutoUpdateAfterCallBack="true" />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;
<anthem:Button ID="BtnBack" runat="server" Text=" 取 消 " CssClass="button" OnClick="BtnBack_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

<%@ Register Assembly="Anthem" Namespace="Anthem" TagPrefix="anthem" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>flash橱窗管理</title>    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">    <meta name="CODE_LANGUAGE" content="C#">    <meta name="vs_defaultClientScript" content="JavaScript">    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">    <link href="../css/css.css" type="text/css" rel="stylesheet"></head><body>    <form id="form1" runat="server">    <div style="text-align:center;">         <anthem:Repeater ID="Repeater1" runat="server" AutoUpdateAfterCallBack="true">                    <HeaderTemplate>                        <table class="DataList" cellspacing="0" rules="all" border="1" id="Datagrid2" style="width: 100%;                            border-collapse: separate;">                            <tr class="DataList_Head" align="Left">                                <td align="center" style="width: 2%;">                                    编号</td>                                <td align="center" style="width: 30%;">                                    标题</td>                                <td align="center" style="width: 30%;">                                    地址</td>                                <td align="center" style="width: 30%;">                                    图片</td>                                <td align="center" style="width: 8%;">                                    操作</td>                            </tr>                    </HeaderTemplate>                    <ItemTemplate>                        <tr class="DataList_Item" align="Left">                            <td align="center" style="width: 2%;">                                    <%#Container.ItemIndex%></td>                            <td align="center" style="width: 30%;">                                <a href=‘<%# DataBinder.Eval(Container.DataItem,"url")%>‘                                    target="_blank"><%# DataBinder.Eval(Container.DataItem,"title")%></a>                            </td>                            <td align="center" style="width: 30%;">                            <a href=‘<%# DataBinder.Eval(Container.DataItem,"url")%>‘                                    target="_blank">                                <%#DataBinder.Eval(Container.DataItem, "url").ToString()%></a>                            </td>                            <td align="center"  style="width: 30%;">                                <a href=‘<%# DataBinder.Eval(Container.DataItem,"name")%>‘                                    target="_blank"><%# DataBinder.Eval(Container.DataItem,"name")%></a>                            </td>                            <td align="center"  style="width: 10%;">                            <anthem:LinkButton Text="修改" ID="btn_Edit" OnClick="btn_Edit_Click" runat="server" ToolTip=‘<%#Container.ItemIndex%>‘></anthem:LinkButton>                                <anthem:LinkButton Text="删除" ID="btn_Delete" OnClick="btn_Delete_Click" PreCallBackFunction="function (){return confirm(‘确定删除?‘);}" runat="server" ToolTip=‘<%#Container.ItemIndex%>‘></anthem:LinkButton>                            </td>                        </tr>                    </ItemTemplate>                    <FooterTemplate>                        </table>                    </FooterTemplate>           </anthem:Repeater>           <table width="90%" cellpadding="5" bgcolor="#e7ecce" border="1">           <tr>                <td align="center" colspan="2">            <anthem:Label ID="Txt_TableTitle" runat="server" AutoUpdateAfterCallBack="true" Text="添加新项"></anthem:Label>            <anthem:HiddenField ID="Txt_ItemIndex" runat="server" AutoUpdateAfterCallBack="true" />                </td>           </tr>            <tr>                <td align="right">                    标题:&nbsp;                </td>                <td align="left">                    <anthem:TextBox ID="Txt_title" runat="server" AutoUpdateAfterCallBack="true" Width="301px"></anthem:TextBox></td>            </tr>            <tr>                <td align="right">                    地址:</td>                <td align="left">                    <anthem:TextBox ID="Txt_url" runat="server" AutoUpdateAfterCallBack="true" Width="301px"></anthem:TextBox></td>            </tr>            <tr>                <td align="right">                    图片地址</td>                <td align="left">                    <anthem:TextBox ID="Txt_name" runat="server" Width="303px" AutoUpdateAfterCallBack="true"></anthem:TextBox></td>            </tr>            <tr>                <td align="center" colspan="2">                    <anthem:Button ID="Btn_Submit" runat="server" Text=" 确 定 " CssClass="button" OnClick="Btn_Submit_Click" AutoUpdateAfterCallBack="true" />                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                    &nbsp; &nbsp;                    <anthem:Button ID="BtnBack" runat="server" Text=" 取 消 " CssClass="button" OnClick="BtnBack_Click" /></td>            </tr>        </table>    </div>    </form></body></html>

然后贴C# code

使用的是Anthem控件,不使用Anthem控件的朋友可以使用常规控件,不影响效果

代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

namespace test
{
public partial class shopWindowMar : System.Web.UI.Page
{
private static string ImgUrl = "/adv_pic";
private static string XmlUrl = "/xml/TheXml.xml";
private static DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ReadXmlInfo();//读取XML文件并绑定到Repeater
}
}

//读取XML文件并绑定到Repeater
public void ReadXmlInfo()
{
ds.Tables.Clear();
ds.ReadXml(Server.MapPath(XmlUrl));
Repeater1.DataSource = ds.Tables[0].DefaultView;
Repeater1.DataBind();
}
//从ds数据源重写XML文件
public void WriteXMLInfo()
{
ds.WriteXml(Server.MapPath(XmlUrl));
}
//删除某节点
protected void btn_Delete_Click(object sender, EventArgs e)
{
Anthem.LinkButton aButton = sender as Anthem.LinkButton;
ds.Tables[0].Rows.RemoveAt(int.Parse(aButton.ToolTip.ToString()));
WriteXMLInfo();
ReadXmlInfo();
}
//修改某节点,初始化修改表单数据
protected void btn_Edit_Click(object sender, EventArgs e)
{
Anthem.LinkButton aButton = sender as Anthem.LinkButton;
int ItemIndex = int.Parse(aButton.ToolTip.ToString());
Txt_TableTitle.Text = "修改现有第" + ItemIndex.ToString() + "个项目";
Btn_Submit.Text = " 修 改 ";
Txt_title.Text = ds.Tables[0].Rows[ItemIndex]["title"].ToString();
Txt_name.Text = ds.Tables[0].Rows[ItemIndex]["name"].ToString();
Txt_url.Text = ds.Tables[0].Rows[ItemIndex]["url"].ToString();
Txt_ItemIndex.Value = ItemIndex.ToString();
}
//提交表单内容,进行新增或修改
protected void Btn_Submit_Click(object sender, EventArgs e)
{
if (Txt_ItemIndex.Value.ToString() != "")
{
int EditIndex = int.Parse(Txt_ItemIndex.Value.ToString());
ds.Tables[0].Rows[EditIndex]["name"] = Txt_name.Text.ToString();
ds.Tables[0].Rows[EditIndex]["title"] = Txt_title.Text.ToString();
ds.Tables[0].Rows[EditIndex]["url"] = Txt_url.Text.ToString();
}
else
{
DataRow dr = ds.Tables[0].NewRow();
dr["name"] = Txt_name.Text.ToString();
dr["title"] = Txt_title.Text.ToString();
dr["url"] = Txt_url.Text.ToString();
ds.Tables[0].Rows.Add(dr);
}
WriteXMLInfo();
ReadXmlInfo();
initTxtTable();
}
//取消修改按钮事件
protected void BtnBack_Click(object sender, EventArgs e)
{
initTxtTable();
}
//重置表单内容
public void initTxtTable()
{
Txt_url.Text = "";
Txt_title.Text = "";
Txt_name.Text = "";
Txt_ItemIndex.Value = "";
Txt_TableTitle.Text = "添加新项";
Btn_Submit.Text = " 添 加 ";
}
}
}

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Xml;using System.IO;

namespace test{    public partial class shopWindowMar : System.Web.UI.Page    {        private static string ImgUrl = "/adv_pic";        private static string XmlUrl = "/xml/TheXml.xml";        private static DataSet ds =  new DataSet();        protected void Page_Load(object sender, EventArgs e)        {            if (!Page.IsPostBack)            {                ReadXmlInfo();//读取XML文件并绑定到Repeater            }        }

        //读取XML文件并绑定到Repeater        public void ReadXmlInfo()        {            ds.Tables.Clear();            ds.ReadXml(Server.MapPath(XmlUrl));            Repeater1.DataSource = ds.Tables[0].DefaultView;            Repeater1.DataBind();        }        //从ds数据源重写XML文件        public void WriteXMLInfo()        {            ds.WriteXml(Server.MapPath(XmlUrl));        }        //删除某节点        protected void btn_Delete_Click(object sender, EventArgs e)        {            Anthem.LinkButton aButton = sender as Anthem.LinkButton;            ds.Tables[0].Rows.RemoveAt(int.Parse(aButton.ToolTip.ToString()));            WriteXMLInfo();            ReadXmlInfo();        }        //修改某节点,初始化修改表单数据        protected void btn_Edit_Click(object sender, EventArgs e)        {            Anthem.LinkButton aButton = sender as Anthem.LinkButton;            int ItemIndex = int.Parse(aButton.ToolTip.ToString());            Txt_TableTitle.Text = "修改现有第" + ItemIndex.ToString() + "个项目";            Btn_Submit.Text = " 修 改 ";            Txt_title.Text = ds.Tables[0].Rows[ItemIndex]["title"].ToString();            Txt_name.Text = ds.Tables[0].Rows[ItemIndex]["name"].ToString();            Txt_url.Text = ds.Tables[0].Rows[ItemIndex]["url"].ToString();            Txt_ItemIndex.Value = ItemIndex.ToString();        }        //提交表单内容,进行新增或修改        protected void Btn_Submit_Click(object sender, EventArgs e)        {            if (Txt_ItemIndex.Value.ToString() != "")            {                int EditIndex = int.Parse(Txt_ItemIndex.Value.ToString());                ds.Tables[0].Rows[EditIndex]["name"] = Txt_name.Text.ToString();                ds.Tables[0].Rows[EditIndex]["title"] = Txt_title.Text.ToString();                ds.Tables[0].Rows[EditIndex]["url"] = Txt_url.Text.ToString();            }            else            {                DataRow dr = ds.Tables[0].NewRow();                dr["name"] = Txt_name.Text.ToString();                dr["title"] = Txt_title.Text.ToString();                dr["url"] = Txt_url.Text.ToString();                ds.Tables[0].Rows.Add(dr);            }            WriteXMLInfo();            ReadXmlInfo();            initTxtTable();        }        //取消修改按钮事件        protected void BtnBack_Click(object sender, EventArgs e)        {            initTxtTable();        }        //重置表单内容        public void initTxtTable()        {            Txt_url.Text = "";            Txt_title.Text = "";            Txt_name.Text = "";            Txt_ItemIndex.Value = "";            Txt_TableTitle.Text = "添加新项";            Btn_Submit.Text = " 添 加 ";        }    }}

转载自:http://www.cnblogs.com/JaggerLee/archive/2009/02/04/1384062.html

时间: 2024-10-08 16:00:47

Repeater、GridView等 操作XML文件的相关文章

Asp.Net 操作XML文件的增删改查 利用GridView

不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="XmlManager.index" %> <!

利用XmlDocument操作XML文件

利用XmlDocument可以方便的操作XML文件. 1.操作XML文件基本方法 (1)添加对System.Xml的引用,并使用using语句添加引用: (2)假设要读取的XML文件如下: <?xml version="1.0" encoding="utf-8"?> <Students> <Student> <Name>张靓靓</Name> <Age>20</Age> <Hob

Java操作XML文件 dom4j 篇

在项目中,我们很多都用到了xml文件,无论是参数配置还是与其它系统的数据交互.今天就来讲一下Java 中使用dom4j来操作XML文件. 我们需要引入的包: //文件包 import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; //工具包 import java.util.Iterator; import java.util.List; //dom4j包 import org.dom

.Net常用技巧_操作xml文件教程(插入节点、修改、删除)

已知有一个XML文件(bookstore.xml)如下:     <?xml   version="1.0"   encoding="gb2312"?>     <bookstore>         <book   genre="fantasy"   ISBN="2-3631-4">             <title>Oberon's   Legacy</title&

springbatch操作XML文件

一.需求分析 使用Spring Batch对XML文件进行读写操作: 从一个xml文件中读取商品信息, 经过简单的处理, 写入另外一个xml文件中. 二.代码实现 1. 代码结构图: 2. applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"

PHP操作XML文件学习笔记

原文:PHP操作XML文件学习笔记 XML文件属于标签语言,可以通过自定义标签存储数据,其主要作用也是作为存储数据. 对于XML的操作包括遍历,生成,修改,删除等其他类似的操作.PHP对于XML的操作方式很多,这次学习的是通过DOMDocument进行操作,其他的操作方法可以参考 http://www.oschina.net/code/snippet_110138_4727 1.对XML文件的遍历 通过DOMDocument对于XML文件的操作的方法:首先要实例化一个DOMDocument类的对

c#操作XML文件的通用方法

c#操作XML文件的通用方法 本文导读:我们在编写C#程序时,经常会通过C#访问XML文件,实现对XML文档的读写操作.下面为大家列出了通用的调用方法,大家可以将这些方法放在共用类里,其它的程序共享调用就可以了. 下面通过一个类将我们平时用c#操作XML文件的通用方法详细的介绍一下,关于asp.net C#操作xml文档实现代码,大家可以参考参考. c# 代码 1 sing System; 2 using System.Data; 3 using System.Configuration; 4

C#操作XML文件实例汇总

针对XML文件的操作是C#程序设计中非常常见的功能.本文即以实例展示了C#操作XML文件的几个常见的示例.具体如下: 1.返回节点下标 public static XmlDocument getDoc(String path)//加载xml文档 { XmlDocument doc = new XmlDocument(); doc.Load(path); return doc; } /// <summary> /// 返回找到的节点下标 /// </summary> /// <

使用dom4j操作xml文件的增删改

package day2.domx; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter;