如题,我们保存数据的方式有很多种。在ASP.NET中,可以通过js赋值隐藏域的方式,也可以通过ViewState,Session这样的内置对象,还可以通过数据库的形式。现在经常用到的就是XML了,它的结构灵活,同时占用的空间很少,也比较容易操作,今天我们就来说说ADO.NET中,如何去操作XML。

  首先我们可以在一个页面上,放入一个GridView用来显示读取的XML的数据(这里使用的是经典的books.xml,在一些网站上可以下载),同时再放入一个富文本框来显示特定的节点,还有一个按钮用于点击后在文本框中显示XML特定节点的内容:

  

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XmlToDataTable.aspx.cs"Inherits="WebApplication1.XmlToDataTable" %><!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></title>
</head>
<body><form id="form1" runat="server"><div><asp:TextBox ID="txtShow" runat="server" TextMode="MultiLine" Width="100%"></asp:TextBox><asp:GridView ID="gvXML" runat="server" AutoGenerateColumns="false"><Columns><asp:BoundField HeaderText="Id" DataField="id" /><asp:BoundField HeaderText="Author" DataField="author" /><asp:BoundField HeaderText="Title" DataField="title" /><asp:BoundField HeaderText="Genre" DataField="genre" /><asp:BoundField HeaderText="Price" DataField="price" /><asp:BoundField HeaderText="Publish_date" DataField="publish_date" /><asp:BoundField HeaderText="Description" DataField="description" /></Columns></asp:GridView><asp:Button ID="btnExport" runat="server" Text="导出XML" OnClick="btnExport_Click" /></div></form>
</body>
</html>

  大家可以看到我在GridView中绑定了一些列,而这些列正是我们的XML拥有的一些节点:

  

<?xml version="1.0"?>
-<catalog> -<book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> +<book id="bk102"> -<book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-11-17</publish_date> <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description> </book> -<book id="bk104"> <author>Corets, Eva</author> <title>Oberon's Legacy</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2001-03-10</publish_date> <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description> </book> -<book id="bk105"> <author>Corets, Eva</author> <title>The Sundered Grail</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2001-09-10</publish_date> <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</description> </book> -<book id="bk106"> <author>Randall, Cynthia</author> <title>Lover Birds</title> <genre>Romance</genre> <price>4.95</price> <publish_date>2000-09-02</publish_date> <description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</description> </book> -<book id="bk107"> <author>Thurman, Paula</author> <title>Splish Splash</title> <genre>Romance</genre> <price>4.95</price> <publish_date>2000-11-02</publish_date> <description>A deep sea diver finds true love twenty thousand leagues beneath the sea.</description> </book> -<book id="bk108"> <author>Knorr, Stefan</author> <title>Creepy Crawlies</title> <genre>Horror</genre> <price>4.95</price> <publish_date>2000-12-06</publish_date> <description>An anthology of horror stories about roaches, centipedes, scorpions and other insects.</description> </book> -<book id="bk109"> <author>Kress, Peter</author> <title>Paradox Lost</title> <genre>Science Fiction</genre> <price>6.95</price> <publish_date>2000-11-02</publish_date> <description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description> </book> -<book id="bk110"> <author>O'Brien, Tim</author> <title>Microsoft .NET: The Programming Bible</title> <genre>Computer</genre> <price>36.95</price> <publish_date>2000-12-09</publish_date> <description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description> </book> -<book id="bk111"> <author>O'Brien, Tim</author> <title>MSXML3: A Comprehensive Guide</title> <genre>Computer</genre> <price>36.95</price> <publish_date>2000-12-01</publish_date> <description>The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.</description> </book> -<book id="bk112"> <author>Galos, Mike</author> <title>Visual Studio 7: A Comprehensive Guide</title> <genre>Computer</genre> <price>49.95</price> <publish_date>2001-04-16</publish_date> <description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description> </book> </catalog>

  嗯,貌似看起来有些乱,不过马上我们处理了之后,它就会变得整齐。接下来在后台我们开始操作XML,首先我们需要让GridView显示数据:首先我们需要建立一个数据集DataSet,然后使用ADO.NET中自带的方法去读取XML,这里的实现很简单:

  

 DataSet ds = new DataSet(); string Path = Server.MapPath("XML/books.xml");ds.ReadXml(Path);

  这样我们的ds中就已经有数据了,接下来绑定GridView后,呈现出来的就是这样:

  

  这时候我们的XML已经读取成功了,接下来我们可以简单的操作下XML中的节点:

  

       private void ShowXML(string Path) {  XmlDocument doc = new XmlDocument();doc.Load(Path);XmlNodeList nodeList = doc.SelectNodes("//title");foreach(XmlNode node in nodeList)            {txtShow.Text += node.InnerXml + "\r\n";}}protected void btnExport_Click(object sender, EventArgs e){string Path = Server.MapPath("XML/books.xml");ShowXML(Path);}

   这里我们用到了XmlDocument类,这个类是用于表示整个XML的DOM,另外一个XmlNode类是表示XML中的一个节点,而XmlNodeList则是表示可以迭代的,XmlNode的一个集合,我们在这里是在加载了DOM对象后,选择了title节点的集合,然后遍历出每个节点下的内容并赋值给txtShow,效果如下:

  

  至此我们已经成功读取到该节点,关于XML的简单操作就介绍到这里。XML的其他操作还有XmlPath的方式,以及各种其他复杂的方式。不过对于XML本身,可以理解为一个DOM,也就是一种特殊的HTML,它拥有自己的标记格式,抛砖引玉,希望大家提出批评和建议。

  ---------------------------------------------------分割线-----------------------------------------------------------

  之前因为手误的原因,在本地的方法中加入了ds这个参数,后来发现文中完全没有用到这个参数,感谢@佩恩六道的指正,原文已经修改。

转载于:https://www.cnblogs.com/wackysoft/p/3821300.html

ADO.NET系列之操作XML相关推荐

  1. SimpleXML系列函数操作XML

    创建SimpleXML对象 使用SimpleXML首先要创建对象.共有3种方法来创建对象,分别是: l  Simplexml_load_file()函数,将指定的文件解析到内存中. l  Simple ...

  2. shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)

    shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)Shell脚本与MySQL数据库交互(增删改查)# 环境准备:安装mariadb 数据库 [root ...

  3. 利用XSLT把ADO记录集转换成XML

    由于XML(可扩展标记语言:eXtensible Markup Language)真正的平台无关性,它正在逐渐成为数据传输的主要介质.XML是一种自描述的语言,数据本身就已经包含了元数据,即关于数据本 ...

  4. shell编程系列23--shell操作数据库实战之mysql命令参数详解

    shell编程系列23--shell操作数据库实战之mysql命令参数详解mysql命令参数详解-u 用户名-p 用户密码-h 服务器ip地址-D 连接的数据库-N 不输出列信息-B 使用tab键代替 ...

  5. C#操作xml之xpath语法

    以前也发过关于.net中操作XML的帖子,但不是很详细,现在我将详细介绍一下c#如何操作xml文件,正如学习操作数据库要学习SQL语言一样,在学习操作xml与语言之前,我们要先熟悉一下xml的&quo ...

  6. .net下操作XML的几篇文章(downmoon收集自MSDN)

    1.通过 XML 发布新闻 http://msdn.microsoft.com/zh-cn/library/ms947599.aspx 2.真实世界的 XML:使用 .NET 框架中集成的读取器和写入 ...

  7. 使用Dom4j操作XML数据

    --------------siwuxie095 dom4j 是一个非常优秀的 Java XML 的 API, 用来读写 XML 文件 和操作 XML 数据 特点:性能优异.功能强大.极端易用 dom ...

  8. 在C#.net中如何操作XML

    在C#.net中如何操作XML 需要添加的命名空间: using System.Xml; 定义几个公共对象: XmlDocument xmldoc ; XmlNode xmlnode ; XmlEle ...

  9. ActionScript 3操作XML 详解

    AS3引入了E4X ,它是根据ECMAScript标准处理XML 数据的全新机制.这使得程序员在程序中无缝地操作XML.在AS3中可以使用XML字面值将XML数据直接写入代码,该字面值将被自动解析. ...

最新文章

  1. spring cloud 日志_微服务架构开发实战:ElasticStack实现日志集中化
  2. Python3 configparser 中文乱码
  3. Java中的一直不能掌握的switch-case语句
  4. word2vec实例详解python_Python实现word2Vec model过程解析
  5. [USACO12FEB]牛的IDCow IDs
  6. 你可能不知道的 10 条 SQL 技巧
  7. UE3 光照 阴影
  8. html 居中 center,html – 如何居中的元素 – 使用什么而不是align:center属性?
  9. C语言实现http的下载
  10. Interesting Finds: 2008.04.06
  11. linux 删除文件 例外,linux 删除文件,某个文件例外
  12. CAM365直播预告|带您全方位了解新一代CAM工具软件
  13. python用于绘制数据图表的是_python图表绘制
  14. 论文解读:《EMDLP:RNA甲基化位点预测的合奏多尺度深度学习模型》
  15. empty是什么意思 php,empty是什么意思中文翻译
  16. 阅读笔记-HTTP返回状态码
  17. 12以内阶乘、自然对数e及e的x次方的计算(Factorial)
  18. 二进制文件漏洞挖掘 IDA插件VulFi安装使用
  19. 合并b站m4s格式的音视频轨道
  20. java的 finalize() 方法

热门文章

  1. 使用soapui测试接口
  2. 美发CRM软件发布了
  3. idea(中英文版)java新建一个运行项目到类(超详细)
  4. 计算机设备管理上显示安全设备,在win10设备管理器驱动程序中显示感叹号的解决方案...
  5. 小码奴历险记:码奴的回忆
  6. 电影服务器点播系统,网上电影注册点播系统
  7. Spark商业案例与性能调优实战100课》第3课:商业案例之通过RDD分析大数据电影点评系各种类型的最喜爱电影TopN及性能优化技巧
  8. 虚拟机MAC地址的分配与修改
  9. Active Spring transaction synchronization or active JTA transaction with specifi
  10. 树莓派做无线网络服务器,树莓派做无线热点