Reading XML Files with the XmlTextReader Class, Part 2

My previous article presented the XmlTextReader class and various code snippets that illustrated how to use it for sequentially reading XML documents or files, determining node types, and parsing for specific nodes and values. This week, I'll cover a few more issues regarding the XmlTextReader class, including ignoring whitespace, skipping to content, and reading attributes.

Reading All Nodes

As a refresherand for those who haven't read the previous articlewhen reading an XML file using the XmlTextReader class, you are forced to read sequentially from the beginning of the file. Here's a sample code snippet where all of the nodes of a specified XML file are read, with each node's NodeType, Name, and Value property displayed:

As you can see, the code simply instantiates an XmlTextReader object (passing it the file name to be read) and then calls the XmlTextReader::Read method until a value of false is returned. As each node is read, the NodeType, Name, and Value properties are then accessed. Figure 1 shows a screen shot of this article's attached demo project, where all nodes are being displayed for the following file:

As you can see in Figure 1, there are quite a few nodes that most applications will not care about the majority of the time. This includes things like the declaration node, comments, and whitespace. The next couple of sections show how to skip over these nodes so that your code can more efficiently concern itself only with nodes containing data (e.g., element and text nodes).

Ignoring Whitespace

Let's first look at how to remove the whitespace nodes. The most obvious way of ignoring these nodes in your code is to insert a conditional statement in your read loop that ignores nodes of type whitespace:

While this does work, it's hardly efficientespecially with extremely large files. Therefore, the XmlTextReader class provides a property called XmlTextReader::WhitespaceHandling that is of type WhitespaceHandling that allows you to specify how you want to deal with whitespace. The valid values are:

All

None

Significant

The All and None enum values refer to either wanting all whitespace nodes (the default) or no whitespace nodes, respectively. The Significant value refers to nodes of type SignificantWhitepsace, which are returned only within the an xml:space='preserve' scope.

To specify that your code does not need to read any whitespace nodes, simply set this attribute accordingly: