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.

str<strong>in</strong>g soapText = Encod<strong>in</strong>g.UTF8.GetStr<strong>in</strong>g(buf);The MemoryStream object reads data only as bytes. Especially <strong>in</strong> a strong-typedenvironment like the .<strong>NET</strong> Framework, an array of bytes and a str<strong>in</strong>g are as different asapples and oranges. Fortunately, the encod<strong>in</strong>g classes provide <strong>for</strong> handy conversionmethods. The Encod<strong>in</strong>g static class belongs to the System.Text namespace.Deserializ<strong>in</strong>g ObjectsTo rebuild a liv<strong>in</strong>g <strong>in</strong>stance of a previously serialized object, you call the Deserializemethod on the specified <strong>for</strong>matter. The deserializer returns an object that you cast tothe particular class type you need, as shown here:StreamReader reader = new StreamReader(filename);Employee emp1 = (Employee) soap.Deserialize(reader.BaseStream);reader.Close();The .<strong>NET</strong> Framework serialization mechanism also allows you to control the postdeserializationprocess<strong>in</strong>g and explicitly handle data be<strong>in</strong>g serialized and deserialized.In this way, you are given a chance to restore transient state and data that, <strong>for</strong> onereason or another, you decide not to serialize. Remember that by mark<strong>in</strong>g a field withthe [NonSerializable]attribute, you keep it out of the serialized stream.By implement<strong>in</strong>g the IDeserializationCallback <strong>in</strong>terface, a class <strong>in</strong>dicates that it wants tobe notified when the deserialization of the entire object is complete. The class caneasily complete the operation by re-creat<strong>in</strong>g parts of the state and add<strong>in</strong>g any<strong>in</strong><strong>for</strong>mation not made serializable. The OnDeserialization method is called after the typehas been deserialized.F<strong>in</strong>ally, it goes without say<strong>in</strong>g that you can't serialize to, say, SOAP, and then pretendto deserialize us<strong>in</strong>g the b<strong>in</strong>ary <strong>for</strong>matter. See the section "Further Read<strong>in</strong>g," on page518, <strong>for</strong> more <strong>in</strong><strong>for</strong>mation about run-time b<strong>in</strong>ary and SOAP serialization.From SOAP to <strong>XML</strong> SerializationA second, very special type of .<strong>NET</strong> Framework serialization is <strong>XML</strong> serialization.Compared to ord<strong>in</strong>ary .<strong>NET</strong> Framework object serialization, <strong>XML</strong> serialization is sodifferent that it shouldn't even be considered another type of <strong>for</strong>matter. It is similar toSOAP and b<strong>in</strong>ary <strong>for</strong>matters because it also persists and restores the object's state, butwhen you exam<strong>in</strong>e the way each serializer works, you see many significant differences.<strong>XML</strong> serialization is handled by us<strong>in</strong>g the XmlSerializer class, which also enables youto control how objects are encoded <strong>in</strong>to elements of an <strong>XML</strong> schema. In addition todifferences <strong>in</strong> goals and implementation details, the strongest difference between runtimeand <strong>XML</strong> serialization is <strong>in</strong> the level of type fidelity they provide.Run-time object serialization guarantees full type fidelity. For this reason, b<strong>in</strong>ary andSOAP serialization are particularly well-suited to preserv<strong>in</strong>g the state of an objectacross multiple <strong>in</strong>vocations of an application. For example, .<strong>NET</strong> Framework remot<strong>in</strong>g(see Chapter 12) uses run-time serialization to marshal objects by value from oneAppDoma<strong>in</strong> to another. Whereas run-time serialization is specifically aimed atserializ<strong>in</strong>g object <strong>in</strong>stances, <strong>XML</strong> serialization is a system-provided (as opposed toobject-provided) mechanism <strong>for</strong> serializ<strong>in</strong>g the data stored <strong>in</strong> an object <strong>in</strong>stance <strong>in</strong>to awell-<strong>for</strong>med schema.The primary goal of <strong>XML</strong> serialization is mak<strong>in</strong>g another application, possibly anapplication runn<strong>in</strong>g on a different plat<strong>for</strong>m, effectively able to consume any stored data.Let's recap the key differences between run-time and <strong>XML</strong> serialization:• Persisted properties Run-time serialization takes <strong>in</strong>to account anyproperties, regardless of the scope a property has <strong>in</strong> the context of theclass. <strong>XML</strong> serialization, on the other hand, avoids private, protected, and391

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

Saved successfully!

Ooh no, something went wrong!