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 11: <strong>XML</strong> SerializationOverviewSerialization is the run-time process that converts an object, or a graph of objects, to al<strong>in</strong>ear sequence of bytes. You can then use the resultant block of memory either <strong>for</strong>storage or <strong>for</strong> transmission over the network on top of a particular protocol. In the<strong>Microsoft</strong> .<strong>NET</strong> Framework, object serialization can have three different output <strong>for</strong>ms:b<strong>in</strong>ary, Simple Object Access Protocol (SOAP), and <strong>XML</strong>. We touched on b<strong>in</strong>aryserialization <strong>in</strong> Chapter 9 while exam<strong>in</strong><strong>in</strong>g how to work around <strong>XML</strong> DiffGram code <strong>in</strong>serialized DataSet and DataTable objects. In this chapter, we'll look briefly at SOAPserialization and then move on to the core topic—<strong>XML</strong> serialization.Run-time object serialization (<strong>for</strong> example, b<strong>in</strong>ary and SOAP) and <strong>XML</strong> serialization aresignificantly different technologies with different implementations and, more important,different goals. Nevertheless, both <strong>for</strong>ms of serialization do just one key th<strong>in</strong>g: theysave the contents and the state of liv<strong>in</strong>g objects out to memory, and from there to anyother storage media. Run-time serialization is governed by .<strong>NET</strong> Framework <strong>for</strong>matterobjects. <strong>XML</strong> serialization takes place under the aegis of the XmlSerializer class.The <strong>XML</strong> serialization process converts the public <strong>in</strong>terface of an object to a particular<strong>XML</strong> schema. Such a mechanism is widely used throughout the .<strong>NET</strong> Framework as away to save the state of an object <strong>in</strong>to a stream or a memory buffer. In Chapter 10, wesaw <strong>XML</strong> serialization used as a way to persist DiffGram with schema scripts thatdescribe a DataSet object. Web services use the XmlSerializer class to encode object<strong>in</strong>stances be<strong>in</strong>g returned by methods.The Object Serialization ProcessIn the .<strong>NET</strong> Framework, object serialization is offered through the classes <strong>in</strong> theSystem.Runtime.Serialization namespace. These classes provide type fidelity andsupport deserialization. As you probably know, the deserialization process is thereverse of serialization. Deserialization takes <strong>in</strong> stored <strong>in</strong><strong>for</strong>mation and recreatesobjects from that <strong>in</strong><strong>for</strong>mation.Object serialization <strong>in</strong> the .<strong>NET</strong> Framework allows you to store public, protected, andprivate fields and automatically handles circular references. A circular reference occurswhen a child object references a parent object and the parent object also referencesthe child object. Serialization classes <strong>in</strong> the .<strong>NET</strong> Framework can detect these circularreferences and resolve them. Serialization can generate output data <strong>in</strong> multiple <strong>for</strong>matsby us<strong>in</strong>g different made-to-measure <strong>for</strong>matter modules. The two system-provided<strong>for</strong>matters are represented by the B<strong>in</strong>aryFormatter and SoapFormatter classes, whichwrite the object's state <strong>in</strong> b<strong>in</strong>ary <strong>for</strong>mat and SOAP <strong>for</strong>mat.Classes make themselves serializable through <strong>for</strong>matters <strong>in</strong> two ways: they can eithersupport the [Serializable]attribute or implement the ISerializable <strong>in</strong>terface. With the[Serializable] attribute, the class author has noth<strong>in</strong>g else to do, as the serialization takesplace governed by caller applications and the class data is obta<strong>in</strong>ed through reflection.The ISerializable <strong>in</strong>terface, on the other hand, enables the class author to exercisecloser control over how the bits of the liv<strong>in</strong>g object are actually persisted.A <strong>for</strong>matter is the .<strong>NET</strong> Framework object that obta<strong>in</strong>s the serialized data from thetarget object. Data is requested either by call<strong>in</strong>g the GetObjectData method on theISerializable <strong>in</strong>terface or through the services of the FormatterServices static class. Inparticular, the GetSerializableMembers method returns all the serializable members <strong>for</strong>a particular class.In the .<strong>NET</strong> Framework, <strong>for</strong>matters are of two types, depend<strong>in</strong>g on the nature of theunderly<strong>in</strong>g stream they use. The b<strong>in</strong>ary <strong>for</strong>matter (available through theB<strong>in</strong>aryFormatter class) saves data to a b<strong>in</strong>ary stream. The SOAP <strong>for</strong>matter (available388

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

Saved successfully!

Ooh no, something went wrong!