Serialization, deserialization, and serialization
<1>
Http://wenku.baidu.com/view/f15ac821dd36a32d737581e0.html
The process of converting an object to a byte sequence is called object serialization. The process of restoring a byte sequence to an object is called object deserialization.
Serialization
Serialize an instance of this type into a file: [MyFile. bin: This File Stores objects persistently after Object serialization.] MyObject obj = new MyObject (); obj. n1 = 1; obj. n2 = 24; obj. str = "some strings"; IFormatter formatter = new BinaryFormatter (); Stream MyStream = new FileStream ("MyFile. bin ", FileMode. create, FileAccess. write, FileShare. none); formatter. serialize (MyStream, obj); stream. close ();
Deserialization
Deserialization here is to put a MyFile. binfile is converted into an obj object through deserialization. [filestream reads the file stream and then uses. net serializer deserialization into objects] IFormatter formatter = new BinaryFormatter (); Stream MyStream = new FileStream ("MyFile. bin ", FileMode. open, FileAccess. read, FileShare. read); MyObject obj = (MyObject) formatter. deserialize (MyStream); stream. close ();
Xml
Serialize such instances into an Xml file XmlSerializer ser = new XmlSerializer (obj. getType (); ser. serialize (new FileStream (@ "users. xml ", FileMode. create), obj); deserialization XmlSerializer serializer = new XmlSerializer (Type. getType ("MyObject"); MyObject my = (MyObject) serializer. deserialize (new FileStream (@ "users. xml ", FileMode. open ));
Zookeeper
Serialization and deserialization are used separately
It is mainly used to store objects in another common format, such as binary, xml, and json. Converting objects into this format is called serialization, deserialization is usually returned from this format.
Serialization is mainly required for cross-platform and Object Storage, because only strings or binary formats are allowed on the network, and files must use binary stream formats, to store an object in memory, you must use serialization to convert the object to xml (string), json (string), or binary (Stream)
C # serialization and deserialization code
UserInFo)
/// <Summary>
/// UserInfo for public test smaple
/// </Summary>
[Serializable]
Public class UserInfo
{
# Region Database fields
Private System. Int32 _ UserID;
Private System. String _ UserName;
Private System. Int16 _ UserType;
Private System. String _ Email;
Private System. String _ Pwd;
Private System. String _ Firstname;
Private System. String _ Lastname;
# Endregion
# Region GETs and SETs
Public System. Int32 UserID
{
Get {return _ UserID ;}
Set {_ UserID = value ;}
}
Public System. String UserName
{
Get {return _ UserName ;}
Set {_ UserName = value ;}
}
Public System. Int16 UserType
{
Get {return _ UserType ;}
Set {_ UserType = value ;}
}
Public System. String Email
{
Get {return _ Email ;}
Set {_ Email = value ;}
}
Public System. String Pwd
{
Get {return _ Pwd ;}
Set {_ Pwd = value ;}
}
Public System. String Firstname
{
Get {return _ Firstname ;}
Set {_ Firstname = value ;}
}
Public System. String Lastname
{
Get {return _ Lastname ;}
Set {_ Lastname = value ;}
}
# Endregion
Public UserInfo ()
{
}
}
First, binary data
Serialize binary code
Public static byte [] Serialize (UserInfo usr)
{
IFormatter formatter = new BinaryFormatter ();
MemoryStream MS = new Me ...... remaining full text>