Light serialization: Only public fields and attributes of serializing classes
Public attributes must be readable and writable.
Use namespace: using system. xml. serialization;
Take the book class as an example:
Serializable template: Public void serializeit (string filename)
{
Xmlserializer serializer = new xmlserializer (typeof (book ));
Streamwriter writer = new streamwriter (filename );
Book mybook = New Book ();
Serializer. serialize (writer, mybook );
Writer. Close ();
}
Deserialization template: Public void deserializeit (string filename)
{
Xmlserializer serializer = new xmlserializer (typeof (book ));
Filestream FS = new filestream (filename, filemode. Open );
Book mybook = (book) serializer. deserialize (FS );
FS. Close ();
}
* The Class Using xmlserializer must have a default constructor without parameters -- Used in the reverse serialization.
* Special processing for serializing the arraylist field X: In the ctor, this. x = new arraylist ();
* If B is referenced in
When serializing A, XML in the following format is generated:
<A>
<B> XXXXX </B>
</A>
When deserializing object A, object a also contains object B with specific values.
* Circular references are not supported in light serialization. That is, when a references B, B also references.
During serialization, the XML format is customized to the SOAP format: Another overload of the xmlserializer constructor: Public void serializeit (string filename)
{
Soapreflectionimporter import = new soapreflectionimporter ();
Xmltypemapping soapmapping = import. importtypemapping (typeof (book ));
Xmlserializer serializer = new xmlserializer (soapmapping );
Streamwriter writer = new streamwriter (filename );
Book mybook = New Book ();
Serializer. serialize (writer, mybook );
Writer. Close ();
}