如题,我们保存数据的方式有很多种。在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>--</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>--</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>--</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这个参数,后来发现文中完全没有用到这个参数,感谢@佩恩六道的指正,原文已经修改。

ADO.NET系列之操作XML的更多相关文章

  1. SimpleXML系列函数操作XML

    创建SimpleXML对象 种方法来创建对象,分别是: l  Simplexml_load_file()函数,将指定的文件解析到内存中. l  Simplexml_load_string()函数,将创 ...

  2. WebService(2)-XML系列之用Stax操作Xml

    源代码下载:链接: http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:利用Stax处理xml文档 一.读取xml 1.基于光标的查找 核心:X ...

  3. ADO.NET系列之Connection对象

    ADO.NET系列之Connection对象 ADO.NET系列之Command对象 ADO.NET系列之DataAdapter对象 ADO.NET系列之事务和调用存储过程 ADO.NET概念 ADO ...

  4. sqlserver 操作xml

    1.xml.exist    输入为XQuery表达式,返回0,1或是Null.0表示不存在,1表示存在,Null表示输入为空 2.xml.value    输入为XQuery表达式,返回一个SQL ...

  5. SQL Server 操作XML数据

    .xml.exist 输入为XQuery表达式,返回0,1或是Null.0表示不存在,1表示存在,Null表示输入为空 .xml.value 输入为XQuery表达式,返回一个SQL Server标量 ...

  6. ADO.NET系列之Command对象

    ADO.NET系列之Connection对象 ADO.NET系列之Command对象 ADO.NET系列之DataAdapter对象 ADO.NET系列之事务和调用存储过程 上一篇<ADO.NE ...

  7. ADO.NET系列之DataAdapter对象

    ADO.NET系列之Connection对象 ADO.NET系列之Command对象 ADO.NET系列之DataAdapter对象 ADO.NET系列之事务和调用存储过程 我们前两篇文章介绍了ADO ...

  8. T-Sql操作Xml数据(转)

    T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...

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

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

随机推荐

  1. cmd界面的编码如何改为utf8

    在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即 ...

  2. javaSE基础——常见的dos命令即其他

     常用的DOS命令 dir(directory) :    列出当前目录下的文件以及文件夹 md(make directory) : 创建目录 rd(remove directory) : 删除目录 ...

  3. 20145236 《Java程序设计》实验一实验报告

    北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.08 实验名称:Java开发环境的熟悉(Linux + Eclipse) 实 ...

  4. ABAP_常用函数整理_傻X版

    输出前导0:CONVERSION_EXIT_ALPHA_INPUT 单位转换:CONVERSION_EXIT_CUNIT_INPUT 单位换算:UNIT_CONVERSION_SIMPLE 修改订单组 ...

  5. Objective-C:Foundation框架-常用类-NSMutableArray

    NSMutableArray是NSArray对的子类,它是可变的,可以随意添加或者删除元素.与Array,也有静态和动态两种创建方法.也可已使用NSArray的方法来创建NSMutableArray. ...

  6. struts2视频学习笔记 21(输入校验的流程)

    课时21 输入校验的流程 1.类型转换器对请求参数执行类型转换,并把转换后的值赋给action中的属性. 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,co ...

  7. Oracle - PL/SQL Commands

    第一章:日志管理 1.forcing log switches sql> alter system switch logfile; 2.forcing checkpoints sql> a ...

  8. 操作系统cmd算法

    实验一  命令解释程序的编写(两周内) 一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS, ...

  9. windows下phpstorm的快捷键

    ctrl+shift+n查找文件 ctrl+shift+f 在一个目录里查找一段代码(ctrl+f的升级版) ctr+shift+r 在一个目录里查找一段代码并替换(ctrl+r的升级版) CTRL+ ...

  10. 常用的JavaScript验证正则表达式1

    匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*评注:表单验证时很实用 匹配网址URL的正则表达式:[a-zA-z]+://[^s]* 评注:网 ...