Xmlvalidatingreader sample code

It's a good idea to catch exceptions here, as several things can go wrong at this stage, including passing the constructor a bad path name.

You can build and run the application from the command line at this stage if you want to check that the file opens correctly.

The following tables list the main properties and methods of the Xml Text Reader class.

The most important function in the second of these tables is Read, which tells the Xml Text Reader to fetch the next node from the document.

The first node is the XML declaration at the top of the document, and that is followed by a comment, whose value is the comment text.

Each XML element in the document will produce a matching pair of Element and End Element nodes, with the content of a node represented by a nested Text node.

It is also going to be easier to use the classes if you include a using directive for the System:: Xml namespace, as shown here.

The Xml Text Reader constructor takes the name of the document you want to parse.

The following exercise will show you how to read an XML document using the Xml Text Reader class.

Here's the sample XML document used by this and the other exercises in this chapter, which lists details of three volcanoes, and which contains many common XML constructs: The code for the XML classes lives in dll, so this needs to be included via a #using directive.

Parsing the file simply means making repeated calls to the Read function until the parser runs out of XML to read.

The simplest way to do this is to put a call to Read inside a while loop.

Once you've got the node, you can use the Node Type property to find out what you have.