A detailed explanation of C # serialization and deserialization meanings
Summarize:
① serialization basically refers to saving an object to a file or stream, such as the ability to serialize the file to be saved to XML, or a disk file
② serialization makes a custom object persistent in some form of storage;
③ Transfer objects from one place to another.
④ converts the value of a class to a generic (that is, a sequential) stream of bytes, which can then be written to a disk file or to any other streaming target.
A ⑥ sequence is the process of storing an object's instance state to a storage medium.
In this procedure, you convert the public fields of the object and the name of the class (including the assembly of the Class) to a byte stream, and then write the byte stream to the data stream. When the object is subsequently deserialized, an identical copy of the original object is created.
⑦ is very useful for data transmission, object storage and so on.
These are I through the online multi-reference and combined with their own experience summarized. Or look at the example.
"Example 1":
Using System;
Using System.IO; File Operation related
Using System.Runtime.Serialization.Formatters.Binary; Contains the BinaryFormatter class, which can be used to serialize and deserialize objects in binary format.
Namespace Serializedeserialize
{
Class Program
{
static void Main (string[] args)
{
Program P = new program ();
P.serializestudent ();
P.deserializestudent ();
}
public void Serializestudent ()
{
Student C = new Student ();
c.id = 0;
C.name = "Xiaojiang";
C.sex = "female";
C.QQ = "676596050";
C.homepage = "Http://hi.baidu.com/jiang_yy_jiang";
Create a binary file Temp.dat
FileStream FileStream = new FileStream ("C:\\temp.dat", FileMode.Create);
BinaryFormatter B = new BinaryFormatter ();
Serializes the student instance object to the FileStream stream: it means that the student object at this time has been stored in the Temp.dat file
B.serialize (FileStream, C);
Filestream.flush ();
Filestream.close ();
Filestream.dispose ();
}
public void Deserializestudent ()
{
Student C = new Student ();
The following three properties are output without changes because deserialization instantiates a new student
C.id = 1;
C.QQ = "676596051";
C.homepage = "Http://www.baidu.com/jiang_yy_jiang";
FileStream FileStream = new FileStream ("C:\\temp.dat", FileMode.Open, FileAccess.Read, fileshare.readwrite);
BinaryFormatter B = new BinaryFormatter ();
Deserializes the Temp.dat file stream into student
c = B.deserialize (FileStream) as Student;
C.name = "Jiangzheng";
C.sex = "male";
Console.Write ("Number:" + c.id + "\ n Name:" + c.name + "\ n Gender:" + C.sex + "\NQQ:" + c.qq + "\ n Home:" + c.homepage);
Console.ReadLine ();
Releasing a file stream resource
Filestream.flush ();
Filestream.close ();
Filestream.dispose ();
}
<summary>
Create 6 readable and writable properties
</summary>
[Serializable]
public class Student
{
Number
private int id;
Name
private string name;
Gender
private string sex;
Qq
private string QQ;
Home
private string homepage;
public int Id
{
get {return ID;}
set {id = value;}
}
public string Name
{
get {return name;}
set {name = value;}
}
public string Sex
{
get {return sex;}
set {sex = value;}
}
public string Qq
{
get {return QQ;}
set {QQ = value;}
}
public String Homepage
{
get {return homepage;}
Set {homepage = value;}
}
}
}
}
The above serialization stores the student class instance in the Temp.dat file, and the reverse deserialization implements the reverse generation of the data in the Temp.dat student object
Easy to understand, in which the C:\temp.dat content in this example is:
Kserializedeserialize, version=1.0.0.0, Culture=neutral, Publickeytoken=null $SerializeDeserialize. program+student ID name sex QQ hao 忔 睙 Lian? 676596050
You can see that it has been converted to binary encoding.
The effect is:
"Example 2":
Using System;
Using System.Collections; The namespace where the HashTable is located
Using System.IO; The namespace where the FileStream is located
Using System.Runtime.Serialization.Formatters.Binary; Serialization of deserialization into a conversion space
Namespace Same1
{
Class Program
{
static void Main (string[] args)
{
Serialize ();
Deserialize ();
Console.ReadLine ();
}
static void Serialize ()
{
Create a Hashtable that contains values that will eventually be serialized
Hashtable addresses = new Hashtable ();
Addresses. ADD ("Jeff", "123 Main Street, Redmond, WA 98052");
Addresses. ADD ("Fred", "987 Pine Road, Phila, PA 19116");
Addresses. ADD ("Mary", "PO Box 112233, Palo Alto, CA 94301");
In order to serialize the Hashtable, you need to create a file Stream
FileStream fs = new FileStream ("DataFile.dat", FileMode.Create);
Serialization of Hashtable into a file stream using binary formatting
BinaryFormatter formatter = new BinaryFormatter ();
Try
{
Formatter. Serialize (FS, addresses);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Console.WriteLine ("Serialization failed because of:" + e.message);
Throw
}
Finally
{
Fs. Flush ();
Fs. Close ();
Fs. Dispose ();
}
}
static void Deserialize ()
{
Declare a Hashtable
Hashtable addresses = null;
Open the file you need to deserialize and output it as a stream
FileStream fs = new FileStream ("DataFile.dat", FileMode.Open);
Try
{
BinaryFormatter formatter = new BinaryFormatter ();
Deserialization file stream is hashtable
Addresses = (Hashtable) formatter. Deserialize (FS);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Console.WriteLine ("Deserialization failed because of:" + e.message);
Throw
}
Finally
{
Fs. Flush ();
Fs. Close ();
Fs. Dispose ();
}
In order to verify the success of deserialization, the key, value pair output in the Hashtale
foreach (DictionaryEntry de in addresses)
{
Console.WriteLine (the birthplace of {0} is: {1}. ", De. Key, DE. Value);
}
}
}
}
DateFile.dat content is:
System.Collections.Hashtable
Loadfactor Version comparer hashcodeprovider hashsize Keys Values system.collections.icomparer$ System.Collections.IHashCodeProvider 霶 8?
Mary Jeff Fred "PO Box 112233, Palo Alto, CA 94301" 123 Main Street, Redmond, WA 98052 987 Pine Road , Phila., PA 19116
The effect is as follows:
Reprint the Meaning of C # serialization and deserialization