C # serialization and deserialization C # serializable Object serialization

Source: Internet
Author: User

C # serialization and deserialization

Using system. io; using system. runtime. serialization. formatters. binary; using system. runtime. serialization. formatters. soap; using system. XML. serialization; using system. runtime. serialization; public class serializeanddeserialize {// 1. Use binaryformatter to serialize public void serializenow () {classtoserialize c = new classtoserialize (); filestream = new filestream ("C: \ temp. dat ", filemode. create); binaryformatter B = new binaryformatter (); B. serialize (filestream, c); filestream. close ();} public void deserializenow () {classtoserialize c = new classtoserialize (); C. sex = "KKKK"; filestream = new filestream ("C: \ temp. dat ", filemode. open, fileaccess. read, fileshare. read); binaryformatter B = new binaryformatter (); C = B. deserialize (filestream) as classtoserialize; console. writeline ("output:" + C. name); console. writeline ("output:" + C. ID); console. writeline ("output:" + C. sex); filestream. close () ;}// 2. Use soapformatter to serialize public void serializenow_soap () {classtoserialize c = new classtoserialize (); filestream = new filestream ("C: \ temp2.dat ", filemode. create); soapformatter S = new soapformatter (); S. serialize (filestream, c); filestream. close ();} public void deserializenow_soap () {classtoserialize c = new classtoserialize (); C. sex = "KKKK"; filestream = new filestream ("C: \ temp2.dat", filemode. open, fileaccess. read, fileshare. read); soapformatter S = new soapformatter (); C = S. deserialize (filestream) as classtoserialize; console. writeline ("output:" + C. name); console. writeline ("output:" + C. ID); console. writeline ("output:" + C. sex); filestream. close () ;}// 3. Use xmlserializer to serialize public void xmlserialize () {person C = new person ("CYJ"); C. courses = new course [2]; C. courses [0] = New Course ("English", "communication tools"); C. courses [1] = New Course ("Mathematics", "Natural Science"); xmlserializer xs = new xmlserializer (typeof (person); stream = new filestream ("C: \ CYJ. html ", filemode. create, fileaccess. write, fileshare. read); Xs. serialize (stream, c); stream. close ();} public void xmldeserialize () {xmlserializer xs = new xmlserializer (typeof (person); stream = new filestream ("C: \ CYJ. XML ", filemode. open, fileaccess. read, fileshare. read); person P = Xs. deserialize (Stream) as person; console. writeline ("output:" + P. name); console. writeline ("output:" + P. age. tostring (); console. writeline ("output:" + P. courses [0]. name); console. writeline ("output:" + P. courses [0]. description); console. writeline ("output:" + P. courses [1]. name); console. writeline ("output:" + P. courses [1]. description); stream. close () ;}// 4. Custom serialization public void otheremployeeclasstest () {employee MP = new employee (); MP. empid = 10; MP. empname = "Qiu Feng"; MP. noserialstring = "hello"; stream steam = file. open ("C: \ temp3.dat", filemode. create); binaryformatter BF = new binaryformatter (); console. writeline ("output:" + "Writing employee info:"); BF. serialize (steam, MP); steam. close (); MP = NULL; // deserialize stream steam2 = file. open ("C: \ temp3.dat", filemode. open); binaryformatter bf2 = new binaryformatter (); console. writeline ("output:" + "reading employee info:"); employee MP2 = (employee) bf2.deserialize (steam2); steam2.close (); console. writeline ("output:" + mp2.empid); console. writeline ("output:" + mp2.empname); console. writeline ("output:" + mp2.noserialstring) ;}} [serializable] public class classtoserialize {public int id = 100; Public string name = "name "; [nonserialized] Public String sex = "male";} [serializable] public class person {private string name; Public string name {get {return name ;} set {name = value ;}} Public String sex; Public int age = 31; public course [] courses; Public Person () {} public person (string name) {name = Name; Sex = "male" ;}} [serializable] public class course {public string name; [xmlignore] Public String description; Public Course () {} public course (string name, string description) {name = Name; description = description;} [serializable] public class employee: iserializable {public int empid = 100; public String empname = "Work hard work smart work happy"; [nonserialized] Public String noserialstring = "noserialstring-test"; public employee () {} private employee (serializationinfo info, streamingcontext ctxt) {empid = (INT) info. getvalue ("employeeid", typeof (INT); empname = (string) info. getvalue ("employeename", typeof (string);} public void getobjectdata (serializationinfo info, streamingcontext ctxt) {info. addvalue ("employeeid", empid); info. addvalue ("employeename", empname );}}

 

 

C # Summary of serial number and deserialization

1. C # serialization and deserialization (this article)

2. Functions of C # serializable Object serialization

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.