不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了

index.aspx 文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="XmlManager.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>XML管理平台(管理员)</title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<%--<asp:BoundField DataField="id" HeaderText="编号" HeaderStyle-Width="100px" />
<asp:BoundField DataField="key" HeaderText="属性名" />
<asp:BoundField DataField="value" HeaderText="属性值" />
<asp:BoundField DataField="explain" HeaderText="说明" />--%>
<asp:TemplateField HeaderText="编号" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="属性名" HeaderStyle-Width="200px">
<EditItemTemplate>
<asp:TextBox ID="txtKey" runat="server" Text='<%# Bind("key") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("key") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="属性值" HeaderStyle-Width="200px">
<EditItemTemplate>
<asp:TextBox ID="txtValue" runat="server" Text='<%# Bind("value") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("value") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="说明" HeaderStyle-Width="200px">
<EditItemTemplate>
<asp:TextBox ID="txtExplain" runat="server" Text='<%# Bind("explain") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("explain") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:CommandField ShowEditButton="True" ShowDeleteButton="true" HeaderText="操作" />
</Columns>
</asp:GridView>
<br />
<br />
<div>
id:<asp:TextBox ID="txt_Id" runat="server"></asp:TextBox>&nbsp;
属性名:<asp:TextBox ID="txt_Key" runat="server"></asp:TextBox>&nbsp;
属性值<asp:TextBox ID="txt_Value" runat="server"></asp:TextBox>&nbsp;
说明:<asp:TextBox ID="txt_Explain" runat="server"></asp:TextBox>&nbsp;
<asp:Button ID="Btn_Add" runat="server" Text="添加" OnClick="Btn_Add_Click" />
</div>
</div>
</form>
</body>
</html>

index.aspx.cs文件

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml; namespace XmlManager
{
public partial class index : System.Web.UI.Page
{
Command com = new Command();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGridView();
}
} //绑定数据
private void LoadGridView()
{
this.GridView1.DataSource = Command.LoadDs().Tables[0];
this.GridView1.DataBind();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
LoadGridView();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Update(e);
GridView1.EditIndex = -1;
LoadGridView();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadGridView();
}
/// <summary>
/// 更新xml数据
/// </summary>
private void Update(GridViewUpdateEventArgs e)
{
string id=((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
string key = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtKey")).Text;
string value = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtValue")).Text;
string explain = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtExplain")).Text;
//Response.Write("<script>alert('" + key+value+explain + "');</script>");
//通过ID获取信息,然后更改信息
XmlDocument doc = new XmlDocument();
doc.Load(com.XmlFilePath());
XmlNode information = doc.SelectSingleNode("information");//查找出根节点
foreach (XmlNode property in information.ChildNodes)
{
XmlNode temp_node = property.FirstChild;
if (temp_node.InnerText == id)
{
//第一种方式
//property.RemoveAll();
//XmlElement ele_id = doc.CreateElement("id");
//ele_id.InnerText = id;
//property.AppendChild(ele_id);
//另外三个属性如上 //第二种方式
property.ChildNodes[1].InnerText = key;
property.ChildNodes[2].InnerText = value;
property.ChildNodes[3].InnerText = explain;
doc.Save(com.XmlFilePath());
break;
}
}
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Command.Delete(this.GridView1, e);
LoadGridView();
}
//添加节点事件
protected void Btn_Add_Click(object sender, EventArgs e)
{
Add();
LoadGridView();
this.txt_Id.Text = "";
this.txt_Key.Text = "";
this.txt_Value.Text = "";
this.txt_Explain.Text = "";
}
//添加
private void Add()
{
string id = this.txt_Id.Text;
string key = this.txt_Key.Text;
string value = this.txt_Value.Text;
string explain = this.txt_Explain.Text;
//从最后一个插入一条数据
XmlDocument doc = new XmlDocument();
doc.Load(com.XmlFilePath());
XmlNode information = doc.SelectSingleNode("information");
XmlElement property = doc.CreateElement("property"); XmlElement ele_id = doc.CreateElement("id");
ele_id.InnerText = id;
property.AppendChild(ele_id); XmlElement ele_key = doc.CreateElement("key");
ele_key.InnerText = key;
property.AppendChild(ele_key); XmlElement ele_value = doc.CreateElement("value");
ele_value.InnerText = value;
property.AppendChild(ele_value); XmlElement ele_explain = doc.CreateElement("explain");
ele_explain.InnerText = explain;
property.AppendChild(ele_explain); information.InsertAfter(property, information.LastChild);
doc.Save(com.XmlFilePath());
}
}
}

Command.cs 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls; namespace XmlManager
{
public class Command:System.Web.UI.Page
{
#region 返回当前XML文件路径
/// <summary>
/// 返回当前XML文件路径
/// </summary>
/// <returns></returns>
public string XmlFilePath()
{
return Server.MapPath("test.xml");
}
#endregion #region 返回加载的XML源
/// <summary>
/// 返回加载的xml源
/// </summary>
/// <returns></returns>
public static DataSet LoadDs()
{
Command com = new Command();
//转换一个XML文件(本地\网络均可)为一个DataSet
DataSet ds = new DataSet();
StringReader sreader = null;
XmlTextReader xtreader = null;
try
{
XmlDocument doc = new XmlDocument();
doc.Load(com.XmlFilePath());
sreader = new StringReader(doc.InnerXml);
xtreader = new XmlTextReader(sreader);
ds.ReadXml(xtreader);
}
catch
{
throw;
}
finally
{
xtreader.Close();
sreader.Close();
}
return ds;
}
#endregion #region 删除XML元素
/// <summary>
/// 删除XML元素
/// </summary>
/// <param name="grid"></param>
/// <param name="e"></param>
public static void Delete(GridView grid, GridViewDeleteEventArgs e)
{
Command com = new Command();
XmlDocument doc = new XmlDocument();
doc.Load(com.XmlFilePath());
XmlNode information = doc.SelectSingleNode("information");
foreach (XmlNode property in information.ChildNodes)
{
XmlNode node_id = property.FirstChild;
if (node_id.InnerText == ((Label)grid.Rows[e.RowIndex].FindControl("Label1")).Text)
{
property.ParentNode.RemoveChild(property);
}
}
doc.Save(com.XmlFilePath());
}
#endregion }
}

UserEdit.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserEdit.aspx.cs" Inherits="XmlManager.UserEdit" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>XML管理平台(用户版)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="编号" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="属性名" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("key") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="属性值" HeaderStyle-Width="200px">
<EditItemTemplate>
<asp:TextBox ID="txtValue" runat="server" Text='<%# Bind("value") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("value") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="说明" HeaderStyle-Width="200px">
<EditItemTemplate>
<asp:TextBox ID="txtExplain" runat="server" Text='<%# Bind("explain") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("explain") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="操作" />
</Columns>
</asp:GridView>
<asp:Button ID="Btn_Down" runat="server" Text="下载配置文件" OnClick="Btn_Down_Click" />
</div>
</form>
</body>
</html>

UserEdit.aspx.cs 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.IO; namespace XmlManager
{
public partial class UserEdit : System.Web.UI.Page
{
Command com = new Command();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGridView();
}
}
//绑定数据
private void LoadGridView()
{
this.GridView1.DataSource = Command.LoadDs().Tables[0];
this.GridView1.DataBind();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
LoadGridView();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Update(e);
GridView1.EditIndex = -1;
LoadGridView();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadGridView();
}
/// <summary>
/// 更新xml数据
/// </summary>
private void Update(GridViewUpdateEventArgs e)
{
string id = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
string key = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text;
string value = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtValue")).Text;
string explain = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtExplain")).Text;
//Response.Write("<script>alert('" + key+value+explain + "');</script>");
//通过ID获取信息,然后更改信息
XmlDocument doc = new XmlDocument();
doc.Load(com.XmlFilePath());
XmlNode information = doc.SelectSingleNode("information");//查找出根节点
foreach (XmlNode property in information.ChildNodes)
{
XmlNode temp_node = property.FirstChild;
if (temp_node.InnerText == id)
{
//第一种方式
//property.RemoveAll();
//XmlElement ele_id = doc.CreateElement("id");
//ele_id.InnerText = id;
//property.AppendChild(ele_id);
//另外三个属性如上 //第二种方式
property.ChildNodes[1].InnerText = key;
property.ChildNodes[2].InnerText = value;
property.ChildNodes[3].InnerText = explain;
doc.Save(com.XmlFilePath());
break;
}
}
}
//流方式下载
protected void Btn_Down_Click(object sender, EventArgs e)
{
string fileName = "user.xml";//客户端保存的文件名
string filePath = com.XmlFilePath();//下载的路径
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";//设置输出流类型——二进制
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}

test.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<information>
<property>
<id>1</id>
<key>神兽</key>
<value>皮卡丘</value>
<explain>来自宠物小精灵</explain>
</property>
<property>
<id>2</id>
<key>神兽</key>
<value>炎帝</value>
<explain>来自宠物小精灵</explain>
</property>
<property>
<id>3</id>
<key>神兽</key>
<value>水君</value>
<explain>来自宠物小精灵</explain>
</property>
<property>
<id>4</id>
<key>神兽</key>
<value>洛基亚</value>
<explain>来自宠物小精灵</explain>
</property>
<property>
<id>5</id>
<key>神兽</key>
<value>金刚武神兽</value>
<explain>来自数码宝贝</explain>
</property>
<property>
<id>6</id>
<key>李逍遥</key>
<value>御剑术</value>
<explain>来自仙剑一的法术</explain>
</property>
<property>
<id>7</id>
<key>李逍遥</key>
<value>万剑诀</value>
<explain>来自仙剑一的法术</explain>
</property>
</information>

Asp.Net 操作XML文件的增删改查 利用GridView的更多相关文章

  1. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  2. 使用dom4j对xml文件进行增删改查

    1.使用dom4j技术对dom_demo.xml进行增删改查 首选要下载dom4j的jar包 在官网上找不到,网上搜索了一下在这个链接:http://sourceforge.net/projects/ ...

  3. Qt操作xml文件(增删改功能)

    这个例子是在根据网上博客<Qt数据库(XML)>改写的一个操作XML的实现. 借鉴了很多里面的代码,大家可以结合上面的博客对照,相信你肯定会对XML的操作熟练起来. 我建立的是Qwidge ...

  4. Java使用DOM4J对XML文件进行增删改查操作

    Java进行XML文件操作,代码如下: package com.founder.mrp.util; import java.io.File; import java.util.ArrayList; i ...

  5. php对xml文件的增删改查

    源文件<?xml version="1.0" encoding="utf-8"?><root>  <endTime>2016 ...

  6. C# 本地xml文件进行增删改查

    项目添加XML文件:FaceXml.xml,并复制到输出目录 FaceXml.xml <?xml version="1.0" encoding="utf-8&quo ...

  7. xml 文件的增删改查

    序列化和反序列化helper using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. java对xml文件做增删改查

    http://www.cnblogs.com/wangchenyang/archive/2011/08/23/2150530.html http://www.blogjava.net/weishuan ...

  9. Python文件操作-文件的增删改查

    需求:对文件进行增删改查 由于时间原因,本次代码没有增加任何注释,如有疑问,请联系编辑者:闫龙 其实我也是醉了,看着这些个代码,我脑袋也特么大了,没办法,大神说了,不让用新知识,只可以使用学过的,所以 ...

随机推荐

  1. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  2. git-简单流程(学习笔记)

    这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...

  3. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

  4. css3中perspective

    perspective 属性定义 3D 元素距视图的距离,以像素计.该属性允许改变 3D 元素查看 3D 元素的视图.当为元素定义 perspective 属性时,其子元素会获得透视效果,而不是元素本 ...

  5. 【C#公共帮助类】 Utils 10年代码,最全的系统帮助类

    为大家分享一下个人的一个Utils系统帮助类,可能有些现在有新的技术替代,自行修改哈~ 这个帮助类主要包含:对象转换处理 .分割字符串.截取字符串.删除最后结尾的一个逗号. 删除最后结尾的指定字符后的 ...

  6. 使用Git Bash远程添加分支和简单部署你的静态页面

    新建一个分支:git branch mybranch(mybranch你的分支名字) 切换到你的新分支: git checkout mybranch 将新分支发布在github上: git push ...

  7. Eclipse出现"Running Android Lint has encountered a problem"解决方案

    安装eclipse for android 时候的错误记录,转载自:http://blog.csdn.net/chenyufeng1991/article/details/47442555 (1)打开 ...

  8. iOS - 模态Model视图跳转和Push视图跳转的混合需求实现原理

    在研发中总会遇到一些莫名的需求,本着存在即合理的态度跟大家分享一下"模态Model视图跳转和Push视图跳转的需求实现",本文仅仅传授研发技术不传授产品以及UE的思想,请大家合理对 ...

  9. iOS10之Expected App Behaviors

    昨天上架到appStore的时候碰到个问题,构建好后上传到itunesconnect的的包都用不了, 显示错误为:此构建版本无效. 或者英文显示为:ITC.apps.preReleaseBuild.e ...

  10. React Native环境配置之Windows版本搭建

    接近年底了,回想这一年都做了啥,学习了啥,然后突然发现,这一年买了不少书,看是看了,就没有完整看完的.悲催. 然后,最近项目也不是很紧了,所以抽空学习了H5.自学啃书还是很无趣的,虽然Head Fir ...