There are three major formats of serialization available: binary, XML and SOAP.
1. Binary Serialization
1.1 Characteristic
· Produces a non-printable sequence of byte-oriented data that represents the source object
· It retains type information within the generated data stream for the source object
1.2 Steps to serialize and deserialize
Before an object can be binary serialized, it must be marked as such by adding the Serializable attribute to the Class as below:
[Serializable]
public class MyBasicData
{
public int IntField1;
public string StringField;
private int IntField2;
public MyBasicData()
{
IntField1 = 100;
StringField = "hello";
IntField2 = 200;
}
}
Then create an instance of the formatter which will perform the work of serializing the class. The binary formatter implements the IFormatter interface; here is the example that can serialize an object to a file by file stream:
MyBasicData myData = new MyBasicData();
FileStream binFileStream = new FileStream("e:\\Bin_ser.bin", FileMode.Create, FileAccess.Write, FileShare.None);
IFormatter binFormatter = new BinaryFormatter();
try
{
binFormatter.Serialize(binFileStream, myData);
binFileStream.Close();
}
catch (SerializationException err)
{
Console.WriteLine(err.Message);
}
We can also use the serialized file to retrieve the object data:
binFileStream = new FileStream("e:\\Bin_ser.bin", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
MyBasicData res = (MyBasicData)binFormatter.Deserialize(binFileStream);
Console.WriteLine(res.IntField1);
1.3 Controlling Binary Serialization
If you don’t want all the class data serialized( such as Password data), there are two ways to realize that. First, using NonSerialized attribute to the member field, the other one is to implement customized ISerializable interface
· Add NonSerialized attribute
[Serializable]
public class MyBasicData
{
public int IntField1;
[NonSerialized]
public string StringField;
private int IntField2;
public MyBasicData()
{
IntField1 = 100;
StringField = "hello";
IntField2 = 200;
}
}
· Implement Customized ISerializable interface
It must implement the following two methods:
void GetObjectData(SerializationInfo info, StreamingContext context)
protected MyObjectConstructor(SerializationInfo info, StreamingContext context)
Detailed info please search from MSDN
2. XML Serialization
When use XML Serialization, it does not need to add Serializable attribute to the serialized class. And it’s very simple to serialize and deserialize objects
2.1 Serialization:
FileStream fileStream = new FileStream("e:\\xml.xml", FileMode.Create, FileAccess.Write, FileShare.None);
XmlSerializer xml = new XmlSerializer(typeof(MyBasicData));
xml.Serialize(fileStream, myData);
2.2 Deserialization:
FileStream fileStream = new FileStream("e:\\xml.xml", FileMode.Open, FileAccess.Write, FileShare.None);
XmlSerializer xml = new XmlSerializer(typeof(MyBasicData));
myData=xml.Serialize(fileStream);
Here we can also use XML attributes to format produced xml file, for example
public class MyBasicData
{
[XmlElement(ElementName = "Inter_Field")]
public int IntField1;
}
It will use Inter_Field to replace of IntField1 attribute in produced xml serialized file
3. SOAP Serialization
It has the same way as binary serialization, just use SoapFormatter to replace of BinaryFormatter, please see the above introduction to Binary Serialization
4. Conclusion
At last, I quote one part of .NET FRAMEWORK NETWORK PROGRAMMING as our conclusion:
The .NET Framework offers three types of serialization, and each has its advantages. Binary serialization is the easiest of all serializes to use and also produces the most compact data. The XML serializer offers interoperability and portability at the expense of a much larger serialized data size. Finally, serializing to SOAP allows interoperability with SOAP-based services such as .NET Remoting and Web services. It’ evident that serialization is a powerful and very useful mechanism for transporting complex data across processes, regardless of whether they’re running on the same machine or across a network.