Basic. Net light serialization

Source: Internet
Author: User
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 ();
}

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.