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.