13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CautionI tried to keep this version of the CSV reader as simple aspossible, which is always a good guidel<strong>in</strong>e. In this case,however, I went beyond my orig<strong>in</strong>al <strong>in</strong>tention and came up with atoo simple reader! Don't be fooled by the fact that the samplecode discussed here works just f<strong>in</strong>e. As I built it, the CSV readerdoes not expose the CSV document as a well-<strong>for</strong>med <strong>XML</strong>document, but rather as a well-<strong>for</strong>med <strong>XML</strong> fragment. There isno root node, and no clear dist<strong>in</strong>ction is made between start andend element tags. In addition, the ReadAttributeValue method isnot supported. As a result, if you use ReadXml to load the CSV<strong>in</strong>to a DataSet object, only the first row would be loaded. If yourun the CsvReader sample <strong>in</strong>cluded <strong>in</strong> this book's sample files,you'll see an additional button on the <strong>for</strong>m labeled UseRead<strong>XML</strong>, which you can use to see this problem <strong>in</strong> action. InChapter 9, after a thorough exam<strong>in</strong>ation of the <strong>in</strong>ternals ofReadXml, we'll build an enhanced version of the CSV reader.The DataGrid control shown <strong>in</strong> Figure 2-2 is read-only, but this does not mean that youcan't modify rows <strong>in</strong> the underly<strong>in</strong>g DataTable object and then save changes back tothe CSV file. One way to accomplish this result would be by us<strong>in</strong>g a customized <strong>XML</strong>writer class—a k<strong>in</strong>d of XmlCsvWriter. You'll learn how to create such a class <strong>in</strong> Chapter4, while we're look<strong>in</strong>g at <strong>XML</strong> writers.NoteThe full source code <strong>for</strong> both the CSV <strong>XML</strong> reader and the sampleapplication mak<strong>in</strong>g use of it is available <strong>in</strong> this book's sample files.The folder of <strong>in</strong>terest is named CsvReader.ImportantThe XmlTextReader class implements a visit<strong>in</strong>g algorithm <strong>for</strong>the <strong>XML</strong> tree based on the so-called node-first approach. Thismeans that <strong>for</strong> each <strong>XML</strong> subtree found, the root is visited first,and then recursively all of its children are visited, from the firstto the last. Node-first is certa<strong>in</strong>ly not the most unique visit<strong>in</strong>galgorithm you can implement, but it turns out to be the mostsensible one <strong>for</strong> <strong>XML</strong> trees.Another well-known visit<strong>in</strong>g algorithm is the <strong>in</strong>-depth-firstapproach, which goes straight to the leaves of the tree andthen pops back to outer parent nodes. The node-first approachis more effective <strong>for</strong> <strong>XML</strong> trees because it visits nodes <strong>in</strong> theorder they are written to disk. Choos<strong>in</strong>g to implement adifferent visit<strong>in</strong>g algorithm would make the code significantlymore complex and less effective from the stand-po<strong>in</strong>t ofmemory footpr<strong>in</strong>t. In short, you should have a good reason toplan and code any algorithm other than node-first.In general, visit<strong>in</strong>g algorithms other than node-first algorithmsexist mostly <strong>for</strong> tree data structures, <strong>in</strong>clud<strong>in</strong>g well-balancedand b<strong>in</strong>ary trees. <strong>XML</strong> files are designed like a tree datastructure but rema<strong>in</strong> a very special type of tree.58

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

Saved successfully!

Ooh no, something went wrong!