C # simple serialization

Source: Internet
Author: User

Serialization: the process of converting the object state to a format that can be maintained or transmitted for two reasons. The first is to permanently Save the data so that it can be rebuilt in the future. The second is to send data from one application domain to another application domain.
Deserialization: Re-constructs the data in the storage medium as an object.

 

First create a class MyObject, as shown in the following code:

MyObjectUsing System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace SerializeTest
{
[Serializable]
Public class MyObject
{
Public int n1 = 0;
Public int n2 = 0;
Public string str = null;
}
}

 

Create a class to write the serialization and deserialization methods. The following Code contains two binary and xml methods.

SerializableCodeUsing System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Using System. Runtime. Serialization;
Using System. Runtime. Serialization. Formatters. Binary; // serialize the namespace to be imported in Binary mode
Using System. Xml. Serialization; // serialize the namespace to be imported in xml format
Namespace SerializeTest
{
Public class Serializable
{
Public void SeriaByBinary ()
{
MyObject obj = new MyObject ();
Obj. n1 = 1;
Obj. n2 = 24;
Obj. str = "binary is good ";
IFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream ("c: \ Myfile. bin", FileMode. Create, FileAccess. Write, FileShare. None );
Formatter. Serialize (stream, obj );
Stream. Close ();
}
Public MyObject DSeriaByBinary ()
{
IFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream ("c: \ Myfile. bin", FileMode. Open, FileAccess. Read, FileShare. Read );
MyObject obj = (MyObject) formatter. Deserialize (stream );
Stream. Close ();
Return obj;
}
Public void SeriaByXml ()
{
MyObject obj = new MyObject ();
Obj. n1 = 1111;
Obj. n2 = 2222;
Obj. str = "xml is great ";
XmlSerializer xmls = new XmlSerializer (typeof (MyObject ));
StreamWriter sw = new StreamWriter ("c: \ myobject. xml ");
Xmls. Serialize (sw, obj );
Sw. Close ();
}
Public MyObject DSeriaByXml ()
{
XmlSerializer xmls = new XmlSerializer (typeof (MyObject ));
FileStream fs = new FileStream ("c: \ myobject. xml", FileMode. Open );
MyObject obj = (MyObject) xmls. Deserialize (fs );
Fs. Close ();
Return obj;
}
}

}

 

Finally, create the TEST form event.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.