15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eading <strong>and</strong> Writing streamed XMl ❘ 907<br />

}<br />

}<br />

richTextBox1.AppendText(rdr.Value + "\r\n");<br />

code download XMLReaderSample.sln<br />

As previously discussed, XmlReader is an abstract class. So in order to use the XmlReader class directly, a<br />

Create static method has been added. The Create method returns an XmlReader object. The overload list<br />

for the Create method contains nine entries. In the preceding example, a string that represents the filename<br />

of the XmlDocument is passed in as a parameter. Stream-based objects <strong>and</strong> TextReader-based objects can<br />

also be passed in.<br />

An XmlReaderSettings object can also be used. XmlReaderSettings specifies the features of the<br />

reader. For example, a schema can be used to validate the stream. Set the Schemas property to a valid<br />

XmlSchemaSet object, which is a cache of XSD schemas. Then the XsdValidate property on the<br />

XmlReaderSettings object can be set to true.<br />

You can use several Ignore properties to control the way the reader processes certain nodes <strong>and</strong> values.<br />

These properties include IgnoreComments, IgnoreIdentityConstraints, IgnoreInlineSchema,<br />

IgnoreProcessingInstructions, IgnoreSchemaLocation, <strong>and</strong> IgnoreWhitespace. These properties<br />

can be used to strip certain items from the document.<br />

read Methods<br />

Several ways exist to move through the document. As shown in the previous example, Read() takes you to<br />

the next node. You can then verify whether the node has a value (HasValue()) or, as you will see shortly,<br />

whether the node has any attributes (HasAttributes()). You can also use the ReadStartElement()<br />

method, which verifies whether the current node is the start element <strong>and</strong> then positions you on the next<br />

node. If you are not on the start element, an XmlException is raised. Calling this method is the same as<br />

calling the IsStartElement() method followed by a Read() method.<br />

ReadElementString() is similar to ReadString(), except that you can optionally pass in the name of an<br />

element. If the next content node is not a start tag, or if the Name parameter does not match the current node<br />

Name, an exception is raised.<br />

Here is an example of how ReadElementString() can be used. Notice that this example uses<br />

FileStreams, so you will need to make sure that you include the System.IO namespace via a using<br />

statement:<br />

private void button6_Click(object sender, EventArgs e)<br />

{<br />

richTextBox1.Clear();<br />

XmlReader rdr = XmlReader.Create("books.xml");<br />

while (!rdr.EOF)<br />

{<br />

//if we hit an element type, try <strong>and</strong> load it in the listbox<br />

if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "title")<br />

{<br />

richTextBox1.AppendText(rdr.ReadElementString() + "\r\n");<br />

}<br />

else<br />

{<br />

//otherwise move on<br />

rdr.Read();<br />

}<br />

}<br />

}<br />

code download XMLReaderSample.sln<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!