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.

908 ❘ ChaPTer 33 mAnipulAtinG xml<br />

In the while loop, you use MoveToContent() to find each node of type XmlNodeType.Element with the<br />

name title. You use the EOF property of the XmlTextReader as the loop condition. If the node is not of<br />

type Element or not named title, the else clause will issue a Read() method to move to the next node.<br />

When you find a node that matches the criteria, you add the result of a ReadElementString() to the list<br />

box. This should leave you with just the book titles in the list box. Note that you don’t have to issue a<br />

Read() call after a successful ReadElementString() because ReadElementString() consumes the entire<br />

Element <strong>and</strong> positions you on the next node.<br />

If you remove && rdr.Name==“title” from the if clause, you will have to catch the XmlException<br />

when it is thrown. If you look at the data file, you will see that the first element that MoveToContent()<br />

will find is the element. Because it is an element, it will pass the check in the if statement.<br />

However, because it does not contain a simple text type, it will cause ReadElementString() to raise an<br />

XmlException. One way to work around this is to put the ReadElementString() call in a function of its<br />

own. Then, if the call to ReadElementString() fails inside this function, you can deal with the error <strong>and</strong><br />

return to the calling function.<br />

Go ahead <strong>and</strong> do this; call this new method LoadTextBox() <strong>and</strong> pass in the XmlTextReader as a<br />

parameter. This is what the LoadTextBox() method looks like with these changes:<br />

private void LoadTextBox(XmlReader reader)<br />

{<br />

try<br />

{<br />

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

}<br />

// if an XmlException is raised, ignore it.<br />

catch(XmlException er){}<br />

}<br />

This section from the previous example:<br />

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

{<br />

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

}<br />

else<br />

{<br />

//otherwise move on<br />

tr.Read();<br />

}<br />

will have to be changed to the following:<br />

if (tr.MoveToContent() == XmlNodeType.Element)<br />

{<br />

LoadTextBox(tr);<br />

}<br />

else<br />

{<br />

//otherwise move on<br />

tr.Read();<br />

}<br />

After running this example, the results should be the same as before. What you are seeing is that there<br />

is more than one way to accomplish the same goal. This is where the flexibility of the classes in the<br />

System.Xml namespace starts to become apparent.<br />

The XmlReader can also read strongly typed data. There are several ReadElementContentAs methods, such<br />

as ReadElementContentAsDouble, ReadElementContentAsBoolean, <strong>and</strong> so on. The following example<br />

shows how to read in the values as a decimal <strong>and</strong> do some math on the value. In this case, the value from the<br />

price element is increased by 25 percent:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!