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.

Chapter 9: ADO.<strong>NET</strong> <strong>XML</strong> Data SerializationOverview<strong>XML</strong> is the key element responsible <strong>for</strong> the greatly improved <strong>in</strong>teroperability of the<strong>Microsoft</strong> ADO.<strong>NET</strong> object model when compared to <strong>Microsoft</strong> ActiveX Data Objects(ADO). In ADO, <strong>XML</strong> was merely an I/O <strong>for</strong>mat (nondefault) used to persist thecontents of a disconnected recordset. The participation of <strong>XML</strong> <strong>in</strong> the build<strong>in</strong>g and <strong>in</strong>the <strong>in</strong>terwork<strong>in</strong>gs of ADO.<strong>NET</strong> is much deeper. The aspects of ADO.<strong>NET</strong> <strong>in</strong> which the<strong>in</strong>teraction and <strong>in</strong>tegration with <strong>XML</strong> is stronger can be summarized <strong>in</strong> two categories:object serialization and remot<strong>in</strong>g and a dual programm<strong>in</strong>g <strong>in</strong>terface.In ADO.<strong>NET</strong>, you have several options <strong>for</strong> sav<strong>in</strong>g objects to, and restor<strong>in</strong>g objects from,<strong>XML</strong> documents. In effect, this capability belongs to one object only—the DataSetobject—but it can be extended to other conta<strong>in</strong>er objects with m<strong>in</strong>imal cod<strong>in</strong>g. Sav<strong>in</strong>gobjects like DataTable and DataView to <strong>XML</strong> is essentially a special case of theDataSet object serialization.As we saw <strong>in</strong> Chapter 8, ADO.<strong>NET</strong> and <strong>XML</strong> classes provide <strong>for</strong> a unified, <strong>in</strong>termediateAPI that is made available to programmers through a dual, synchronized programm<strong>in</strong>g<strong>in</strong>terface—the XmlDataDocument class. You can access and update data us<strong>in</strong>g eitherthe hierarchical node-based approach of <strong>XML</strong> or the relational approach of columnbasedtabular data sets. At any time, you can switch from a DataSet representation ofthe data to an <strong>XML</strong> Document Object Model (<strong>XML</strong> DOM) representation, and viceversa. Data is synchronized, and any change you enter <strong>in</strong> either model is immediatelyreflected and visible <strong>in</strong> the other.In this chapter, we'll explore the <strong>XML</strong> features built around the DataSet object and otherADO.<strong>NET</strong> objects <strong>for</strong> data serialization and deserialization. You'll learn how to persistand restore data contents, how to deal with schema <strong>in</strong><strong>for</strong>mation, and even how schema<strong>in</strong><strong>for</strong>mation is automatically <strong>in</strong>ferred from the <strong>XML</strong> source.Serializ<strong>in</strong>g DataSet ObjectsLike any other .<strong>NET</strong> Framework object, a DataSet object is stored <strong>in</strong> memory <strong>in</strong> ab<strong>in</strong>ary <strong>for</strong>mat. Unlike other objects, however, the DataSet object is always remoted andserialized <strong>in</strong> a special <strong>XML</strong> <strong>for</strong>mat, called a DiffGram. (We'll look at the DiffGram <strong>for</strong>matand the relative API <strong>in</strong> more detail <strong>in</strong> Chapter 10.) When the DataSet object trespassesacross the boundaries of the application doma<strong>in</strong>s (AppDoma<strong>in</strong>s), or the physicalborders of the mach<strong>in</strong>e, it is automatically rendered as a DiffGram. At its dest<strong>in</strong>ation,the DataSet object is silently rebuilt as a b<strong>in</strong>ary and immediately usable object.In ADO.<strong>NET</strong>, serialization of an object is per<strong>for</strong>med either through the publicISerializable <strong>in</strong>terface or through public methods that expose the object's <strong>in</strong>ternalserialization mechanism. As .<strong>NET</strong> Framework objects, ADO.<strong>NET</strong> objects can plug <strong>in</strong>tothe standard .<strong>NET</strong> Framework serialization mechanism and output their contents tostandard and user-def<strong>in</strong>ed <strong>for</strong>matters. The .<strong>NET</strong> Framework provides a couple of built<strong>in</strong><strong>for</strong>matters: the b<strong>in</strong>ary <strong>for</strong>matter and the Simple Object Access Protocol (SOAP)<strong>for</strong>matter. A .<strong>NET</strong> Framework object makes itself serializable by implement<strong>in</strong>g themethods of the ISerializable <strong>in</strong>terface—specifically, the GetObjectData method, plus aparticular flavor of the constructor. Accord<strong>in</strong>g to this def<strong>in</strong>ition, both the DataSet and theDataTable objects are serializable.In addition to the official serialization <strong>in</strong>terface, the DataSet object supplies analternative, and more direct, series of methods to serialize and deserialize itself, but <strong>in</strong> aclass-def<strong>in</strong>ed <strong>XML</strong> <strong>for</strong>mat only. To serialize us<strong>in</strong>g the standard method, you create<strong>in</strong>stances of the <strong>for</strong>matter object of choice (b<strong>in</strong>ary, SOAP, or whatever) and let the324

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

Saved successfully!

Ooh no, something went wrong!