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.

In the next few sections of this chapter, you'll learn how to build a custom <strong>XML</strong> readerwork<strong>in</strong>g on top of comma-delimited CSV files.Mapp<strong>in</strong>g CSV Files to <strong>XML</strong>A CSV file consists of one or more l<strong>in</strong>es of text. Each l<strong>in</strong>e conta<strong>in</strong>s str<strong>in</strong>gs of textseparated by commas. Each l<strong>in</strong>e of a CSV file can be naturally associated with adatabase row <strong>in</strong> which each token maps to a column. Likewise, a l<strong>in</strong>e <strong>in</strong> a CSV file canalso be correlated to an <strong>XML</strong> node with as many attributes as the comma-separatedtokens. The follow<strong>in</strong>g code shows a typical CSV file:Davolio,Nancy,Sales RepresentativeFuller,Andrew,Sales ManagerLeverl<strong>in</strong>g,Janet,Sales RepresentativeA good <strong>XML</strong> representation of this structure is shown here:Each row <strong>in</strong> the CSV file becomes a node <strong>in</strong> the <strong>XML</strong> representation, while each tokenis represented by a node attribute. In this case, the <strong>XML</strong> schema is ever-chang<strong>in</strong>gbecause the number of attributes varies with the number of commas <strong>in</strong> the CSV file.The number of total columns can be stored as an extra property. You can opt <strong>for</strong> anautomatically generated sequence of attribute names such as col1, col2, and so on, orif the CSV file provides a header with column names, you can use those names. Ofcourse, there is no way to know <strong>in</strong> advance, and <strong>in</strong> general, whether the first row has tobe read as the first data row or just the header. A possible workaround is add<strong>in</strong>g anextra property that tells the reader how to handle the first row.Us<strong>in</strong>g the <strong>XML</strong> schema described so far, you can use the follow<strong>in</strong>g pseudocode to readabout a given item of <strong>in</strong><strong>for</strong>mation <strong>in</strong> the second row:XmlCsvReader reader = new XmlCsvReader("employees.csv");reader.Read();reader.Read();Console.WriteL<strong>in</strong>e(reader[1].Value);Console.WriteL<strong>in</strong>e(reader["col2"].Value);Another reasonable <strong>XML</strong> schema <strong>for</strong> a CSV file is shown here:DavolioNancySales Representative46

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

Saved successfully!

Ooh no, something went wrong!