C # serialization and deserialization

Source: Internet
Author: User

the definition and application of serialization and deserialization can be used on the Internet. I am not talking about the following nonsense:
. NET provides three serialization methods: 1. xml serializer 2. Soap serializer 3. binaryserializer
in this example, we use 1. xml serializer
problem: when the database records constraints on a product, this constraint increases with the recognition of products and the number of product constraints.
if you use column fields to record the constraints of this product, you need to constantly adjust the table structure. This causes a lot of problems for the entire development process. trouble
we can define a constraint class and serialize this class into an XML file to be saved to the database. We can deserialize it.
the constraint can be added at any time (only more than the previous one) multiple but not fewer) an error occurs when a serialized XML deserialization is deleted.
This reduces the trouble caused by operations on database fields.

define the constraint class and mark it with the serializable attribute -- this class can be serialized as follows:
[serializable]
public class batchconstraints
{< br> Public datetime from {Get; set ;}
[xmlignore]
Public timespan periodtime {Get; Set ;}< br> Public int batchcount {Get; set ;}
[xmlelement ("periodtime")]
Public String period
{< br> Get
{< br> return periodtime. tostring ();
}< br> set
{< br> periodtime = timespan. parse (value);
}< BR >}< br> // constraints can be added at any time
}< br> During serialization, the results are as follows:

2009-04-14t00: 00: 00
1
1.00: 00: 00

Note: 1. the timespan type cannot be serialized. We set xmlignore for public timespan periodtime {Get; set,
Periodtime is not serialized during serialization. We can convert timespan to string type and save it to the database. For example, [xmlelement ("periodtime")]
Public String Period
2. constraints can be added at any time (only more than the previous items but not less). An error occurs when a serialized XML deserialization is deleted.
The method is as follows:

Code

  //  Serialization  
Batchconstraints mschedule = New Batchconstraints ();
Mschedule. From = Registerdate;
Mschedule. Period = Period;
Mschedule. batchcount = Batchcount;
Xelement mbatchpublishschedule = Xmlhelp. xmlserializer (mschedule );
// Deserialization
Batchconstraints settinglist = Xmlhelp. xmldeserialize < Batchconstraints > (Xelement (XML Field Retrieved from the database ));

/// <Summary>
/// Serialization
/// </Summary>
/// <Param name = "OBJ"> </param>
/// <Returns> </returns>
Public Static Xelement xmlserializer < T > (T obj)
{
Try
{
Ser = New Xmlserializer (obj. GetType ());
Stream stream = New Memorystream ();
Ser. serialize (stream, OBJ );
Stream. Seek ( 0 , Seekorigin. Begin );
Using (Streamreader Reader = New Streamreader (Stream ))
{
_ Xelement = Xelement. Load (Reader );
}
Stream. Close ();
Return _ Xelement;
}
Catch
{
Return Null ;
}
}
/// <Summary>
/// Deserialization
/// </Summary>
Public Static T xmldeserialize < T > (Xelement Xe)
{
Try
{
Ser = New Xmlserializer ( Typeof (T ));
Return (T) SER. deserialize (Xe. createreader ());
}
Catch
{
Return Default (T );
}
}

 

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.