How to use XmlReader to read XML files in C

  • 2021-10-11 19:19:31
  • OfStack

XmlReader provides us with a minimal resource-consuming way to parse XML data by reading the document forward and identifying the read elements. Most of the time, we use XmlReader to verify the data validity of XML file (use Read () method of XmlReader instance to read all nodes in turn to determine whether they conform to the specified schema). Using this non-cache, read-only, forward-only way, only a small amount of data is put into memory every time you read, which occupies a small amount of memory. It is the best choice for reading XML files with large contents.

Let's look at the steps for the XmlReader class to read the XML file:

1. Create an instance of the XmlReader class by using the Create () factory method, and pass the read XML file name into the method as a parameter;

2. Establish a loop that repeatedly calls the Read () method. This method starts at the first node of the file and then reads all the remaining nodes, but only one node per call. Returns True if there is a node that can be read, and False when the end of the file is reached;

3. In this loop, the properties and methods of the XmlReader instance are examined for information about the current node (node type, name, data, and so on). Continuously execute the loop until Read () returns False;

Let's start with an example:

Employees. xml file:


<?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 code:


<%@ 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>

Output:

= > employees
==== > employee
======= > name
========== > firstName
========== > lastName
======= > city
======= > state
======= > zipCode
==== > employee
======= > name
========== > firstName
========== > lastName
======= > city
======= > state
======= > zipCode

Let's look at the properties and methods of the XmlReader class:

属 性 说 明
AttributeCount 返回当前节点的属性个数
Depth 返回当前节点的深度,用于判断指定的节点是否具有子节点
EOF 判断读取器是否位于流的末端
HasAttribute 返回指示当前节点是否具有属性的布尔值
HasValue 返回指示当前节点是否具有值的布尔值
IsEmptyElement 判断当前节点是否是1个空元素
LocalName 返回当前节点的本地名称
Name 返回当前节点的限定名称
NamespaceURI 返回当前节点的命名空间URI
NodeType 以XmlNodeType枚举的形式返回当前节点的节点类型
Prefix 返回与当前节点相关的命名空间前缀
ReadState 以ReadState枚举的形式返回读取器的当前状态
Settings 返回用于创建XmlReader实例的XmlReaderSettings对象
Value 返回当前节点的值
ValueType 获得当前节点的CLR类型

Important methods for the XmlReader class:

方 法 说 明
Close 通过将ReadState枚举设置为Closed来关闭Xmlreader对象
Create 创建XmlReader对象的实例并将其返回给调用程序
GetAttribute 获得属性的值
IsStartElement 指示当前节点是否是开始标签
MoveToAttribute 移动读取器到指定的属性
MoveToContent 如果当前节点不是内容节点,则移动读取器至下1个内容节点
MoveToElement 移动读取器至包含当前属性的元素;用于列举属性以及想切换至包含所有这些属性的元素
MoveToFirstAttribute 移动读取器至当前节点的第1个属性
MoveToNextAttribute 移动读取器至当前节点的下1个属性
Read 从流中读取下1个节点
ReadContentAs 读取提供类型的对象的内容
ReadElementContentAs 读取当前元素并返回指定类型对象的内容
ReadEndElement 移动读取器越过当前结束标签并移动到下1个节点
ReadInnerXml 以字符串的形式读取包括标记在内的当前节点所有内容
ReadOutXml 读取包括当前节点标记和子节点在内的节点的内容
ReadToDescendant 移动读取器至下1个匹配子孙元素的节点
ReadToFollowing 不断读取直至找到指定的元素
ReadToNextSlibing 移动读取器至下1个匹配兄弟元素的节点
ReadValueChunk 允许读取嵌入在XML文档中的大型文本流

Members of the XmlNodeType enumeration:

成 员 说 明
Attribute 属性
CDATA CDATA区域
Comment XML注释
Document 文档对象,表示XML树的根
DocumentFragment 文档片断
DocumentType 文档类型声明
Element,EndElement 开始元素和结束元素
Entity,EndEntity 开始实体声明和结束实体声明
EntityReference 实体引用(如&lt;)
None 有没有读取节点而查询节点类型时使用
Notation DTD中的符号条目
ProcessingInstruction XML处理指令
SignificantWhitespace 在混合内容模型文档中的空白,或者设置了xml:space=preserve时使用
Text 元素的文本内容
Whitespace 标记之间的空白
XmlDeclaration 在文档顶部的XML声明

Important properties of the XmlReaderSettings class:

属 性 说 明
CheckCharacters 允许你获得或者设置用于指示是否执行字符检查的值
ConformanceLevel 获得或设置XmlReader对象的符合要求
IgnoreComment 允许你获得或设置用于指示是否忽略注释的值
IgnoreProcessingInstruction 指定是否忽略处理指令
IgnoreWhitespace 指定是否忽略无意义的空格
ProhibitDtd 指定是否允许DTD处理
Schemas 指定在执行XML验证时使用的XmlSchemaSet
ValidationFlags 获得或者设置用于指定模式验证设置的值
ValidationType 获得或者设置用于指定所执行的验证类型的值
XmlResolver 设置用于访问外部文档的XmlReslover

With the XmlReaderSettings class, you can specify a series of functions supported by the XmlReader object by simply passing XmlReaderSettings as a parameter into the Create () method of XmlReader. As shown below:


<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>

To sum up, we can use XmlReader class to read XML files in a non-cached, read-only and forward-only way. This method occupies less memory and is recommended for everyone to use.


Related articles: