XmlReader provides us with a way to parse XML data with the least resource consumption by reading the document forward and identifying the elements read. Many times we use XmlReader to verify the data validity of XML files (using the Read() method of the XmlReader instance to read all nodes in turn to determine whether they meet the specified pattern). Using this non-cache, read-only, forward-only method, only a small amount of data is put into memory each time you read, which consumes less memory, making it an optimal choice for reading XML files with larger content.
Let's take a look at the steps to read XML files in the XmlReader class:
1. Use the Create() factory method of the XmlReader class to create an instance of the class and pass the read XML file name into the method as a parameter;
2. Create a loop that repeatedly calls Read() method. This method starts with the first node of the file and then reads all remaining nodes, but only one node is read per call. Return True if there is a node that can be read, and False when the file is last;
3. In this loop, the properties and methods of the XmlReader instance will be checked to obtain information about the current node (type, name, data, etc.). Continuously execute the loop until Read() returns False;
Let’s first look at an example:
document:
<?xml version='1.0'?> <employees> <employee > <name> <firstName>Nancy</firstName> <lastName>Davolio</lastName> </name> <city>Seattle</city> <state>WA</state> <zipCode>98122</zipCode> </employee> <employee > <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="" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { //Location of XML file string xmlFilePath = ("~/"); try { using (XmlReader reader = (xmlFilePath)) { string result; while (()) { //Process only the elements if ( == ) { result = ""; for (int count = 1; count <= ; count++) { result += "==="; } result += "=> " + + "<br/>"; += result; } } } } catch (Exception ex) { = "An Exception occurred: " + ; } } </script> <html xmlns="http:///1999/xhtml" > <head runat="server"> <title>Reading an XML File using XmlReader</title> </head> <body> <form runat="server"> <div> <asp:label runat="server" /> </div> </form> </body> </html>
Output result:
=> employees
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
Let's take a look at the properties and methods of the XmlReader class:
Attributes | illustrate |
---|---|
AttributeCount | Returns the number of attributes of the current node |
Depth | Returns the depth of the current node, used to determine whether the specified node has child nodes |
EOF | Determine whether the reader is at the end of the stream |
HasAttribute | Returns a boolean value indicating whether the current node has attributes |
HasValue | Returns a boolean value indicating whether the current node has a value |
IsEmptyElement | Determine whether the current node is an empty element |
LocalName | Return the local name of the current node |
Name | Returns the qualified name of the current node |
NamespaceURI | Returns the namespace URI of the current node |
NodeType | Return the node type of the current node in the form of an XmlNodeType enum |
Prefix | Returns the namespace prefix associated with the current node |
ReadState | Return the current state of the reader as a ReadState enum |
Settings | Returns the XmlReaderSettings object used to create an XmlReader instance |
Value | Return the value of the current node |
ValueType | Obtain the CLR type of the current node |
Important methods of XmlReader class:
method | illustrate |
---|---|
Close | Close the Xmlreader object by setting the ReadState enum to Closed |
Create | Create an instance of the XmlReader object and return it to the calling program |
GetAttribute | Get the value of the attribute |
IsStartElement | Indicates whether the current node is the start tag |
MoveToAttribute | Move the reader to the specified property |
MoveToContent | If the current node is not a content node, move the reader to the next content node |
MoveToElement | Move the reader to the element containing the current attribute; to list the attributes and to the element containing all of these attributes |
MoveToFirstAttribute | Move the first property of the reader to the current node |
MoveToNextAttribute | Move the reader to the next property of the current node |
Read | Read the next node from the stream |
ReadContentAs | Read the content of an object of the type |
ReadElementContentAs | Read the current element and return the contents of the object of the specified type |
ReadEndElement | Move the reader over the current end tag and move to the next node |
ReadInnerXml | Read all contents of the current node including tags as a string |
ReadOutXml | Read the contents of nodes including the current node tag and child nodes |
ReadToDescendant | Move the reader to the next node matching the descendant element |
ReadToFollowing | Continuously read until the specified element is found |
ReadToNextSlibing | Move the reader to the next node matching the sibling element |
ReadValueChunk | Allows reading of large text streams embedded in XML documents |
Members of XmlNodeType enum:
Member | illustrate |
---|---|
Attribute | property |
CDATA | CDATA Area |
Comment | XML Comments |
Document | Document object, representing the root of the XML tree |
DocumentFragment | Document fragments |
DocumentType | Document type declaration |
Element,EndElement | Start element and end element |
Entity,EndEntity | Start entity declaration and end entity declaration |
EntityReference | Entity reference (such as <) |
None | Is there a node read and query node type used? |
Notation | Symbol entries in DTD |
ProcessingInstruction | XML processing instructions |
SignificantWhitespace | Used when blanks in the mixed content model document, or when xml:space=preserve is set |
Text | Text content of the element |
Whitespace | Blank between marks |
XmlDeclaration | XML declaration at the top of the document |
Important properties of XmlReaderSettings class:
Attributes | illustrate |
---|---|
CheckCharacters | Allows you to obtain or set values that indicate whether to perform character checks |
ConformanceLevel | Obtain or set the XmlReader object to meet the requirements |
IgnoreComment | Allows you to obtain or set values to indicate whether to ignore comments |
IgnoreProcessingInstruct |
Specify whether to ignore processing instructions |
IgnoreWhitespace | Specify whether to ignore meaningless spaces |
ProhibitDtd | Specify whether DTD processing is allowed |
Schemas | Specify the XmlSchemaSet to use when performing XML verification |
ValidationFlags | Obtain or set the value used to specify the mode verification settings |
ValidationType | Obtain or set the value used to specify the type of verification performed |
XmlResolver | Set up XmlReslover for accessing external documents |
Through the XmlReaderSettings class, you can specify a series of functions supported by the XmlReader object. To do this, just pass 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 = ("~/"); //Create the XmlReaderSettings object and set appropriate properties XmlReaderSettings settings = new XmlReaderSettings(); = true; = true; try { //Get reference to the XmlReader object using (XmlReader reader = (xmlFilePath, settings)) { string result; while (()) { //Process only the elements if ( == ) { //Reset the variable for a new element result = ""; for (int count = 1; count <= ; count++) { result += "==="; } result += "=> " + + "<br/>"; += result; } } } } catch (Exception ex) { = "An Exception occurred: " + ; } } </script>
In summary, we can use the XmlReader class to read XML files in a non-cache, read-only, and forward-only manner. This method takes up less memory and is recommended for everyone to use.