Serialization and deserialization of XML application settings classes

Source: Internet
Author: User
Tags bool config return serialization
Xml| Program 1 public class ApplicationSettings
2 {
3
4 private bool appsettingschanged;
5//variable for storing application settings.
6
7 Private point formlocation;
8
9 Public Point Formlocation
10 {
One get {return formlocation;}
Set
13 {
if (value!= formlocation)
15 {
Formlocation = value;
appSettingsChanged = true;
18}
19}
20}
21st
22
23//Deserialize the class from the configuration file.
public bool Loadappsettings ()
25 {
XmlSerializer myserializer = null;
FileStream myFileStream = null;
BOOL FileExists = false;
29
Try
31 {
32//Create XmlSerializer for applicationsettings type.
Myserializer = new XmlSerializer (typeof (ApplicationSettings));
FileInfo fi = new FileInfo (application.localuserappdatapath
+ @ "\myapplication.config");
36//If the configuration file exists, open it.
Panax Notoginseng if (fi. Exists)
38 {
myFileStream = fi. OpenRead ();
40//Deserialize the configuration file to create a new
A//ApplicationSettings instance.
ApplicationSettings myappsettings =
(ApplicationSettings) Myserializer.deserialize (
myFileStream);
45//For this instance of the ApplicationSettings class
46//Assign attribute value.
This.formlocation = myappsettings.formlocation;
FileExists = true;
49}
50}
Wuyi catch (Exception ex)
52 {
MessageBox.Show (ex. message);
54}
Finally
56 {
57//If FileStream is turned on, turn it off.
(myFileStream!= null)
59 {
Myfilestream.close ();
61}
62}
63
64
FileExists return;
66}
67
68//If the settings are changed, the
The 69//class is serialized into the configuration file.
public bool Saveappsettings ()
71 {
if (this.appsettingschanged)
73 {
StreamWriter mywriter = null;
XmlSerializer myserializer = null;
A try
77 {
78//For ApplicationSettings type
79//Create XmlSerializer.
Myserializer = new XmlSerializer (
Bayi typeof (ApplicationSettings));
Mywriter =
New StreamWriter (Application.LocalUserAppDataPath
+ @ "\myapplication.config", false);
85//This instance of the ApplicationSettings class
86//Serialization to the configuration file.
Myserializer.serialize (Mywriter, this);
88}
(Exception ex)
90 {
MessageBox.Show (ex. message);
92}
The finally
94 {
95//If FileStream is turned on, turn it off.
(Mywriter!= null)
97 {
Mywriter.close ();
99}
100}
101}
102 return appsettingschanged;
103}
104}

Source: Ting BLOG



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.