XmlReader通过向前读取文档并识别读取到的元素,为我们提供了一种消耗资源最少的方式来解析XML数据。很多时候我们都是利用XmlReader来对XML文件的数据有效性进行验证(使用XmlReader实例的Read()方法依次读取所有节点,以此判断是否与符合指定的模式)。使用这种非缓存、只读、只向前的方式,每次读取只将很少的数据放入内存,对内存的占用量较小,对于读取内容较大的XML文件不失为一种最佳的选择。
让我们看看XmlReader类读取XML文件的步骤:
1、使用XmlReader类的Create()工厂方法创建该类的一个实例,并将被读取的XML文件名作为参数传入方法;
2、建立一个反复调用Read()方法的循环。这个方法从文件的第一个节点开始,然后读取所有余下的节点,但每次调用只读取一个节点。如果存在一个节点可被读取则返回True,而当到达文件最后时则返回False;
3、在这个循环中,将检查XmlReader实例的属性和方法,以获得关于当前节点的信息(节点的类型、名称、数据等)。不断执行循环直到Read()返回False;
下面首先看一个示例:
Employees.xml文件:
<?xml version='1.0'?> <employees> <employee id="1"> <name> <firstName>Nancy</firstName> <lastName>Davolio</lastName> </name> <city>Seattle</city> <state>WA</state> <zipCode>98122</zipCode> </employee> <employee id="2"> <name> <firstName>Andrew</firstName> <lastName>Fuller</lastName> </name> <city>Tacoma</city> <state>WA</state> <zipCode>98401</zipCode> </employee> </employees>
aspx代码:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
//Location of XML file
string xmlFilePath = Server.MapPath("~/Employees.xml");
try
{
using (XmlReader reader = XmlReader.Create(xmlFilePath))
{
string result;
while (reader.Read())
{
//Process only the elements
if (reader.NodeType == XmlNodeType.Element)
{
result = "";
for (int count = 1; count <= reader.Depth; count++)
{
result += "===";
}
result += "=> " + reader.Name + "<br/>";
lblResult.Text += result;
}
}
}
}
catch (Exception ex)
{
lblResult.Text = "An Exception occurred: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Reading an XML File using XmlReader</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="lblResult" runat="server" />
</div>
</form>
</body>
</html>
输出结果:
=> employees
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
下面让我们看看XmlReader类的属性和方法:
XmlReader类的重要方法:
XmlNodeType枚举的成员:
XmlReaderSettings类的重要属性:
通过XmlReaderSettings类,你可以指定一系列由XmlReader对象支持的功能,为此,只需将XmlReaderSettings作为参数传入XmlReader的Create()方法中即可。如下所示:
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string xmlFilePath = Server.Mappath("~/Employees.xml");
//Create the XmlReaderSettings object and set appropriate properties
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
try
{
//Get reference to the XmlReader object
using (XmlReader reader = XmlReader.Create(xmlFilePath, settings))
{
string result;
while (reader.Read())
{
//Process only the elements
if (reader.NodeType == XmlNodeType.Element)
{
//Reset the variable for a new element
result = "";
for (int count = 1; count <= reader.Depth; count++)
{
result += "===";
}
result += "=> " + reader.Name + "<br/>";
lblResult.Text += result;
}
}
}
}
catch (Exception ex)
{
lblResult.Text = "An Exception occurred: " + ex.Message;
}
}
</script>
总结下来,我们可以使用XmlReader类以非缓存、只读、只向前的方式读取XML文件,这种方法占用内存少,推荐大家使用。
