This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

SERIALIZATION IN C#
(1 vote, average: 5.00 out of 5)
Written by Christopher   
Sunday, 15 June 2008 15:23

INTRODUCTION

Many applications need to communicate with other applications to exchange data or store data in the hard disk. The .Net framework has made these tasks quite simple by including the serialization technique, which can convert objects into binary, SOAP(Simple Object Access Protocol ) or XML documents in order to provide a convenient way for storing and transferring information. Besides the three mentioned format types you can also implement custom serialization to take complete control of the serialization output.


SERIALIZING OBJECTS

Serialization is a technique which converts an object to a pre-defined format for transmission across a network or hdd storage. We do not care how the object is stored on the system’s memory since we can define the output format of the serialization. After serialization, the object can be recreated by using the deserialization methods. A class can be defined as being subject to serialization by including an optional attribute in its declaration. You can also disable the serialization of any members you do not need to include in the output document. When creating a class it is a good practice to mark it as “Serializable”. Even if you don’t need to serialize it now you might need it later. In order to have access to the serialization methods you must use the namespace System.Runtime.Serialization. 

 

SERIALIZATION FORMATS

In order to serialize an object you must choose between various types of formatting:

·         BinaryFormat: It is located in the System.Runtime.Serialization.Formatters.Binary namespace and it is the most efficient way of serializing objects. A human cannot directly read the contents of the object in this format, since they are printed in binary strings. It is mainly used for communication between .Net applications.

·         SoapFormat:  It is located in the System.Runtime.Serialization.Formaters.Soap namespace. It is an XML based format, but not identical to XML format. It is usually used for transmitting objects through a network and is more likely to traverse firewalls than the Binary format.

·         XMLFormat: XML is a standardized format for storing application readable information. It can be used to store any kind of data such as music files, office documents, pictures etc… .Net includes the namespace System.Xml.Serialization for using XML methods in order to convert objects to and from XML documents. It is supported by different applications and operating systems. However, XML can serialize only public data and you cannot serialize object graphs.

It is a good practice to use SoaP format for compatibility and Binary format for efficiency. The XML format is not as rich as the other two but you need not to worry about incompatibilities with this serialization format.

 

COMPATIBILITY ISSUES

You need to carefully implement the serialization techniques in later versions of your programs. Suppose you have a serializable class to be serialized with the newest version of your program. The customer takes the serialized data and tries to deserialize them with an older version of the program. The difference between the two of them is that in the new version a new field has been added to the class. The programmer must define the new field as optional by using the “OptionalField” attribute. This way when the deserialization process sees that it doesn’t have this field in the serialized data it will just place a “null” value in it instead of throwing an exception. Besides that, you should never remove a serialized field or change its name and type.

 

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options