Serialization and deserialization of XML source files in C #

Source: Internet
Author: User
Tags object serialization

During system development, system configuration parameters are often stored in XML files. The advantage is that you do not need to read the database when calling parameters, in addition, the system parameter cache can be used to set file dependencies.

However, this data storage method may also cause some problems.

We usually set and modify system parameters in the background of the system. However, due to business needs, the background of the system and the foreground are not in the same website directory or even on the same server. In this way, the foreground or other parts of the system cannot call the XML file set by the background system parameters.

Therefore, you can save serialized XML files in the database.

 

Built-in serialization and deserialization methods in. net. If you do not know, you can check msdn.

The disadvantage of these methods is that only entity classes can be serialized as physical files and physical files can be serialized as objects, which causes a serious problem, if we want to save the XML file to the database, we need to read another file. Seriously AffectedProgramPerformance, increase server load.

 

I have recently studied discuz nt!CodeThe two methods exist in the bottom layer of discuz:

///   <Summary>
/// Object serialization into XML source files
///   </Summary>
///   <Param name = "OBJ"> Object </Param>
///   <Returns> XML source file string </Returns>
Public   Static   String Serialize ( Object OBJ)
{
String Returnstr =   "" ;
Xmlserializer serializer = Getserializer (obj. GetType ());
Memorystream MS =   New Memorystream ();
Xmltextwriter xtw =   Null ;
Streamreader SR =   Null ;
Try
{
Xtw =   New System. xml. xmltextwriter (MS, encoding. utf8 );
Xtw. Formatting = System. xml. Formatting. indented;
Serializer. serialize (xtw, OBJ );
Ms. Seek ( 0 , Seekorigin. Begin );
Sr =   New Streamreader (MS );
Returnstr = Sr. readtoend ();
}
Catch (Exception ex)
{
Throw Ex;
}
Finally
{
If (Xtw ! =   Null )
Xtw. Close ();
If (SR ! =   Null )
Sr. Close ();
Ms. Close ();
}
Return Returnstr;

}

///   <Summary>
/// XML source file deserialization into Entity
///   </Summary>
///   <Param name = "type"> Entity type </Param>
///   <Param name = "S"> XML source file </Param>
///   <Returns> </returns>
Public   Static   Object Deserialize (type, String S)
{
Byte [] B = System. Text. encoding. utf8.getbytes (s );
Try
{
Xmlserializer serializer = Getserializer (type );
Return Serializer. deserialize ( New Memorystream (B ));
}
Catch (Exception ex)
{
Throw Ex;
}
}

 

For reference only.

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.