AbstractSerialization in C# .NET plays a key role in various functions, such as remoting. Developers may often need to perform custom serialization in order to have complete control over the serialization and deserialization processes. Binary .NET serialization processes may not be enough to deserialize an input in one platform with serialized output from another. In the second article of this series, Anupam Banerji explains XML serialization, schemas, and provides an example of XML serialization. IntroductionXML serialization is a method of storing object states in a portable, human-readable format. An XML serialized object can be deserialized by any platform, and not just the .NET platform as required by an object serialized in the .NET binary format. XML serialization can also be standardized using an XML schema. This allows serialization for business processes that operate across multiple operating systems, platforms, or have multiple development parties. XML serialization has a few disadvantages when compared to binary serialization. The object itself is serialized, not the entire object graph. Another disadvantage is that only public fields may be serialized. Therefore, object design requires a careful consideration of XML serialization. The advantages of XML serialization should be a decisive factor in the integration of XML serialization in object design and implementation. (De)Serializing an ObjectSerialization is performed by instancing an XmlSerializer object. The serialized output is written to an underlying stream object. This is identical to the process used in binary serialization. The deserialized object should be cast into the original data type (a generic object type is returned upon deserialization). The code below serializes a double:using System.Xml.Serialization;
using System.IO;XmlSerializer xs = new XmlSerializer(typeof(double));
FileStream fs = new FileStream(, FileMode.Create); xs.Serialize(fs, (double)(10));
fs.Close();
The file contains XML output with the object state in a node:<?xml version="1.0"?><double>10</double>To deserialize an object back into the original data type (notice the cast): double d = (double)xs.Deserialize(fs);One of the drawbacks of using the XML serializer is that it writes only one object state to the output rather than multiple object states. This is similar to the binary serializer. I don’t necessarily agree with Microsoft® on the implementation of the Serialize() method; a large number of files are needed to serialize a large number of objects. The other issue is that deserialization of the object cannot be implemented without reading the contents of the correct object into the underlying stream, even if multiple object states were written to a single file. A class may also be serialized. The class and serialized members must be marked as public. The class must also have a parameterless constructor; this may be overloaded. The [Serializable] attribute does not have to be applied to the class, unlike binary serialization. Private and protected members are ignored, and any inherited fields are not serialized. (De)Serializing a DataSetAn DataSet object may also be (de)serialized using the XmlSerializer object. There are two methods to accomplish this.The Developer can choose to implement the method to (de)serialize an object after instancing a DataSet object and populating it. The second method is to use DataSet methods. Read more: Codeproject
using System.IO;XmlSerializer xs = new XmlSerializer(typeof(double));
FileStream fs = new FileStream(, FileMode.Create); xs.Serialize(fs, (double)(10));
fs.Close();
The file contains XML output with the object state in a node:<?xml version="1.0"?><double>10</double>To deserialize an object back into the original data type (notice the cast): double d = (double)xs.Deserialize(fs);One of the drawbacks of using the XML serializer is that it writes only one object state to the output rather than multiple object states. This is similar to the binary serializer. I don’t necessarily agree with Microsoft® on the implementation of the Serialize() method; a large number of files are needed to serialize a large number of objects. The other issue is that deserialization of the object cannot be implemented without reading the contents of the correct object into the underlying stream, even if multiple object states were written to a single file. A class may also be serialized. The class and serialized members must be marked as public. The class must also have a parameterless constructor; this may be overloaded. The [Serializable] attribute does not have to be applied to the class, unlike binary serialization. Private and protected members are ignored, and any inherited fields are not serialized. (De)Serializing a DataSetAn DataSet object may also be (de)serialized using the XmlSerializer object. There are two methods to accomplish this.The Developer can choose to implement the method to (de)serialize an object after instancing a DataSet object and populating it. The second method is to use DataSet methods. Read more: Codeproject
0 comments:
Post a Comment