不废话,直接上如何利用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文件(本地\网络均可)为一个DataSetDataSet 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>

转载于:https://www.cnblogs.com/jianxuanbing/p/5528985.html

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

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

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

  2. [PYTHON] 对XML文件进行增删改查操作

    PYTHON 操作 XML 读取XML文件 关于XML的介绍 <data> 与 </data> 是一对标签的开始与结束 <property - /> 也是一个正确的 ...

  3. 对xml文件的增删改查及读写

    示例1 1.xml 是QT 另外需要手动添加的模块. 在pro 文件中需要手动添加: QT += xml 2.xml头文件需要用到#include<QtXml> , 当然这是包含了xml ...

  4. dom4j创建、解析xml文件(增删改查)

    先对xml文件进行解析,xml文件如下图所示 <?xml version="1.0" encoding="UTF-8"?> <?eclipse ...

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

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

  6. adodb mysql.inc.php,php adodb操作mysql数据库示例(增删改查)

    php adodb操作mysql数据库示例(增删改查) 发布于 2014-10-05 08:16:18 | 113 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: H ...

  7. Mybatis入门:2(xml形式的增删改查)

    xml形式的增删改查 这里感觉没啥好讲的,照着代码自己敲一遍.认真再看看应该都懂的. Maven工程坐标 <?xml version="1.0" encoding=" ...

  8. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查

    首页 > 技术 > 编程 > NET > 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查 前端使用AngularJS的$res ...

  9. PHP操作Mysql简单的增删改查

    PHP操作Mysql简单的增删改查 今天晚上回想了一下php怎么操作mysql,突然发现都忘了,然后通过回忆,搜寻资料总结了以下几点,一是提醒自己不要忘了,再就是希望能帮助一些有帮助的朋友.大佬请路过 ...

最新文章

  1. TFTP commons-net-3.3.jar
  2. 您的关注是我最大的快乐
  3. java中静态变量,静态代码块,静态方法,实例变量,匿名代码块的加载顺序
  4. 技术交底软件_【干货分享】软件类产品如何进行专利挖掘与技术交底书撰写?...
  5. ids和ips主要区别在于_接口测试和功能测试的区别
  6. Python BeautifuSoup4 爬表格
  7. 微信小程序搭建tabbar
  8. BCNet实现PLC数据采集解决方案,BCNet
  9. restsharp text html,c# – 使用RestSharp发送HTTP POST Multipart / form-data字段
  10. c语言中闰年的流程图_c语言(算法流程图).ppt
  11. 微信输入几个字,就能查看好友朋友圈所有动态!你不会不知道吧
  12. Your Command Line Tools are too outdated
  13. matlab里pascal是什么意思_台球里的自然角是什么意思
  14. 远程ntp服务器响应模式6查询,H3C WP5048无线PoE注入器 命令参考-Release 2208-6W100
  15. Latex编辑器Texstudio的注释快捷键。
  16. ARM Neon Intrinsics 学习指北:从入门、进阶到学个通透
  17. mysql复制表以及复制数据库
  18. 用imspost制作catia后处理_基于IMS POST五轴海德汉系统后处理的开发
  19. 思科模拟器教程-静态NAT网络地址转换
  20. DEFLATE压缩数据格式规范 v1.3

热门文章

  1. 产品精益画布 Lean Canvas
  2. POJ 3208 Apocalypse Someday
  3. vmos切换安卓版本_虚拟大师VMOS——手机里的quot;虚拟机quot;
  4. Servlet生命周期及请求、响应
  5. VS2012+OpenGL (涵盖GLUT+GLEW+FreeGlut+GLTools) h lib 和dll文件
  6. 医用超声阵列换能器波束容差分析与变迹处理
  7. php curlclose,PHP curl_close函数 - PHP 教程 - 自强学堂
  8. MicroPython和MQTT云端迷你气象站
  9. winForm 编程 视频教学
  10. java公交查询系统开题报告_毕业设计论文-基于JAVA的公交查询系统的设计与实现.doc...