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.

The <strong>XML</strong> reader will loop through the nodes, mov<strong>in</strong>g from one record to the next, asshown here:void ProcessNotes(XmlTextReader reader){try{while(reader.Read()){if (reader.NodeType == XmlNodeType.Text)MessageBox.Show(reader.Value);}}catch {}f<strong>in</strong>ally{MessageBox.Show("Closed...");}}The connection rema<strong>in</strong>s open until the reader is closed. Next store the results <strong>in</strong> astr<strong>in</strong>g variable and use that str<strong>in</strong>g to create a new XmlTextReader object. (See Chapter2.) This technique gives you an extra advantage: you can work with the reader whileyou are disconnected from the database.An <strong>XML</strong> Reader <strong>for</strong> Data ReadersAn <strong>XML</strong> reader can work on top of different data conta<strong>in</strong>ers, <strong>in</strong>clud<strong>in</strong>g streams, files,and text readers. By writ<strong>in</strong>g a custom <strong>XML</strong> reader, you can also navigate non-<strong>XML</strong> dataus<strong>in</strong>g the same <strong>XML</strong> reader metaphor. In this case, you create a virtual <strong>XML</strong> tree andmake the underly<strong>in</strong>g data look like <strong>XML</strong>. (In Chapter 2, you learned how to visit CSVfiles the <strong>XML</strong> way.)The ability to expose result sets via <strong>XML</strong> is specific to SQL Server 2000 and potentiallyto any other native managed provider <strong>for</strong> DBMS systems with full support <strong>for</strong> <strong>XML</strong>queries. You can't, <strong>for</strong> example, use the ExecuteXmlReader method with an object ofclass OleDbCommand.Recall from the section "Under the Hood of ExecuteXmlReader," on page 366, the<strong>in</strong>ternal structure of ExecuteXmlReader. The ExecuteXmlReader method simplycreates an <strong>XML</strong> text reader based on the <strong>in</strong>ternal stream used to carry data back and<strong>for</strong>th. What about creat<strong>in</strong>g a custom <strong>XML</strong> reader by build<strong>in</strong>g a virtual <strong>XML</strong> tree aroundthe provider-specific data reader? In this way, you could easily extend any .<strong>NET</strong>Framework data provider by us<strong>in</strong>g the ExecuteXmlReader method. This method is notas effective as us<strong>in</strong>g the <strong>in</strong>ternal stream, but it does work and can be applied to all dataproviders.Build<strong>in</strong>g the <strong>XML</strong> Data ReaderLet's rework the CSV reader example from Chapter 2 and build an XmlDataReaderclass <strong>in</strong>herit<strong>in</strong>g from XmlReader, as follows:public class XmlDataReader : XmlReader{299

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

Saved successfully!

Ooh no, something went wrong!