On the serialization and de-serialization

First to tell you about Serialization is used to doing, why he appeared

Serialization This technology is actually the final analysis, the temporary data stored on the computer. We all know that for a fleeting program object data, not just the program restart, restart the computer, and even change the internal function may also lead to the disappearance of the object, but there are some objects that you do not want it random disappearance and want to load the next time you open the program, in this environment - it appeared serialized, its meaning is to keep the object persistence

Here I introduce to you about C # among XML serialization and binary serialization, almost all the way serialization are similar therefore we focus on two of the more common serialization

  Suppose class name: Person object named: P

A: XML Serialization

  The first step of course is to introduce the namespace: Using System.IO; Using System.Xml.Serialization;

  To instantiate our class -XmlSerializer

XmlSerializer xml = new XmlSerializer(typeof(Person));

Examples of parameters of a class type Type, using typeof on it, the class name passed inside typeof

  Create FileStream object starts to hit the target after xml

FileStream fs = new FileStream("asd.xml",FileMode.OpenOrCreate);

  Examples of this type parameter passed in when there are two, the first is that you play as a file name -string, the second is your IO operation, usually the default is FileMode.OpenOrCreat

  ~ Is then serialized method -Serialize

xml.Serialize(fs,P);

Serialize using two parameters of this method are the name of the object name -fs instance of FileStream object and you want to save -P

Upon completion of this approach, our XML serialization is over ~

 

We want to deserialize words are simple, it is not the same as the last method invoked in this method -Doserialize

Object obj = xml.Doserialize(fs);

This way as long as the incoming object is instantiated FileStream name of -fs on it, he has a return value of type Object, accept it and then put it into a strong type you need to OK

Two: Binary Serialization

  Binary serialization, and XML serialization infrastructure almost

  First is the introduction of the namespace: Using System.IO;

                Using System.Runtime.Senalization.Formatter.Binary;

  To instantiate our class -BinaryFormatter

BinaryFormatter bin = new BinaryFormatter();

Examples of when such parameters do not oh ~

Then create FileStream objects

FileStream fs = new FileStream("asd.xml",FileMode.OpenOrCreate);

Examples of this type parameter passed in when there are two, the first is that you play as a file name -string, the second is your IO operation, usually the default is FileMode.OpenOrCreat

  ~ Is then serialized method -Serialize

bin.Serialize(fs,P);

Serialize using two parameters of this method are the name of the object name -fs instance of FileStream object and you want to save -P

Upon completion of this approach, our binary serialization is over ~

 

Object obj = bin.Doserialize(fs);

This way as long as the incoming object is instantiated FileStream name of -fs on it, he has a return value of type Object, accept it and then put it into a strong type you need to OK

  Although both are mostly the same, but more and more binary serialization requirements, as follows:

1) To a sequence of the type, the type and the type of the parent class of all the members of all types are marked as required:

  [Serializable]

2) binary serialization can serialize the class field, if some of the fields do not want it there as long as the sequence of marked:

  [NonSerialized]

Guess you like

Origin www.cnblogs.com/namelessblog/p/11565909.html