[Serialization] Summary, gradually improving

Source: Internet
Author: User

I checked the information on the Internet and there are multiple methods for serialization. Here I write a new method that I just used to generate an XML file. If you need it, you can Baidu another method.

1. Add reference "using system. xml. serialization ;";

2. Use "[serializable]" to mark the class to be serialized, as shown in figure

 
[Serializable] public class logservice {Public String strname; Public String strimage; Public String strtoolnum; public list <mainproperty> mainpropertylist ;}

3. assign values to all the variables in the class "logservice" to be serialized in step 1, including "list <childproperty> childpropertylist ";

4. Add the following code in the code location where the XML serialization file needs to be generated, for example, in the Click Event of the "serialization" button:

Xmlserializer xs = new xmlserializer (typeof (logservice); stream = new filestream (save path (including XML file name and suffix), filemode. create, fileaccess. write, fileshare. read); Xs. serialize (stream, the assigned "logservice" instance); stream. close ();

At this point, the serialization of an XML file is complete, and the file is deserialized below;

5. The deserialization code is as follows:

 
Xmlserializer xs = new xmlserializer (typeof (logservice); stream = new filestream (file storage path (including XML file name and suffix), filemode. open, fileaccess. read, fileshare. read); logservice logs = Xs. deserialize (Stream) as logservice;

In this way, the serialized file will be read into the "logs" instance of the "logservice" class.

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.