Reading Notes: Liang Jian. Net -- converts a class into XML and stores the read-only Class Object for deserialization.

Source: Internet
Author: User

Resume A serializable class

 

///   <Summary>
/// The configured modul class, that is, the saved configuration item (which can be customized as your own items)
/// Note the types of relevant attributes and [xmlelement]
///   </Summary>
Public   Class Modulesettings
{
Private   String Server;
Private   String Username;
Private   String Password;
Private   String Database;

[Xmlelement]
Public   String Server
{
Set {Server = Value ;}
Get { Return Server ;}
}
[Xmlelement]
Public   String Username
{
Set {Username = Value ;}
Get { Return Username ;}
}
[Xmlelement]
Public   String Password
{
Set {Password = Value ;}
Get { Return Password ;}
}
[Xmlelement]
Public   String Database
{
Set {Database = Value ;}
Get { Return Database ;}
}
}

 

Save after serialization

 

Public   Static   Void Savesettings (modulesettings data)
{
String Filename = Httpcontext. Current. server. mappath (getsettingsfile ());
Xmlserializer serializer =   New Xmlserializer ( Typeof (Modulesettings ));

//Serialize the object
Filestream FS= NewFilestream (filename, filemode. Create );
Serializer. serialize (FS, data );
FS. Close ();
}

 

Deserialization after reading

 

 

// Obtain the instance information of the configuration item object.
Public   Static Modulesettings getsettings ()
{
Xmlserializer serializer =   New Xmlserializer ( Typeof (Modulesettings ));
Try
{
// Retrieve from File
String Filename = Httpcontext. Current. server. mappath (getsettingsfile ());
Filestream FS =   New Filestream (filename, filemode. Open );
Return Data = (Modulesettings) serializer. deserialize (FS ); // Deserialization
FS. Close ();
}
Catch (System. Io. filenotfoundexception)
{
// If the object does not exist, an empty object is returned.
Return Data =   Null
}
}

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.