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

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

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

Writ<strong>in</strong>g an XSLT style sheet to trans<strong>for</strong>m a DataSet object <strong>in</strong>to a Recordset object isuseful <strong>for</strong> other reasons as well. First, the style sheet code needed is not trivial andrequires a good work<strong>in</strong>g knowledge of both XPath and XSLT. Look at it as a usefulexercise to test your level of familiarity with the technologies. Second, you can applythe style sheet directly to the b<strong>in</strong>ary DataSet object, without first serializ<strong>in</strong>g the object to<strong>XML</strong>.The ability to style a b<strong>in</strong>ary DataSet object is provided by the XmlDataDocument class.As mentioned <strong>in</strong> Chapter 6, XmlDataDocument is an XPath document class. Itimplements the IXPathNavigable <strong>in</strong>terface and, as such, can be directly passed as anargument to the Trans<strong>for</strong>m method. (We'll exam<strong>in</strong>e the XmlDataDocument class <strong>in</strong>detail <strong>in</strong> Chapter 8.)Gett<strong>in</strong>g the DataSet ObjectThe follow<strong>in</strong>g code fetches some records from the Northw<strong>in</strong>d database's Employeestable and stores them <strong>in</strong>to a DataSet object:str<strong>in</strong>g conn = "DATABASE=northw<strong>in</strong>d;SERVER=localhost;UID=sa;";str<strong>in</strong>g comm = "SELECT firstname, lastname, title, notes FROMemployees";SqlDataAdapter adapter = new SqlDataAdapter(comm, conn);DataSet data = new DataSet("Northw<strong>in</strong>d");adapter.Fill(data, "Employees");The DataSet object is named Northw<strong>in</strong>d and conta<strong>in</strong>s just one DataTable object,Employees. As we'll see <strong>in</strong> a moment, the names of the DataSet and DataTable objectsplay a key role <strong>in</strong> the <strong>XML</strong> representation of the objects. By default, a DataSet object isnamed NewDataSet, and a DataTable object is named Table. (We'll look at ADO.<strong>NET</strong><strong>XML</strong> serialization <strong>in</strong> great detail <strong>in</strong> Chapter 9 and Chapter 10.)The <strong>XML</strong> representation of a DataSet object looks like this:......⋮⋮TipYou can get the str<strong>in</strong>g represent<strong>in</strong>g the <strong>XML</strong> version of the DataSetobject through the DataSet method GetXml. The text does not <strong>in</strong>cludeschema <strong>in</strong><strong>for</strong>mation. You can get the schema script separately bycall<strong>in</strong>g the GetXmlSchema method. To persist the <strong>XML</strong>representation to a stream, use the WriteXml method <strong>in</strong>stead.Trans<strong>for</strong>m<strong>in</strong>g the DataSet ObjectTrans<strong>for</strong>m<strong>in</strong>g a DataSet object <strong>in</strong>to a Recordset object poses a couple of problems.The first is that you have to <strong>in</strong>fer and write the Recordset object's schema. The secondis that the <strong>XML</strong> layout of the DataSet object depends on a number of differentparameters. In particular, the root of the <strong>XML</strong> version of the DataSet object depends on267

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

Saved successfully!

Ooh no, something went wrong!