首页 > 开发 > XML > 正文

序列化和反序列化XML应用程序设置类

2024-09-05 20:56:01
字体:
来源:转载
供稿:网友
 1        public class applicationsettings
  2        {
  3
  4            private bool appsettingschanged;
  5            // 用于存储应用程序设置的变量。
  6
  7            private point formlocation;
  8       
  9            public point formlocation
 10            {
 11                get { return formlocation; }
 12                set
 13                {
 14                    if (value != formlocation)
 15                    {
 16                        formlocation = value;
 17                        appsettingschanged = true;
 18                    }
 19                }
 20            }
 21
 22
 23            // 从配置文件中反序列化类。
 24            public bool loadappsettings()
 25            {
 26                xmlserializer myserializer = null;
 27                filestream myfilestream = null;
 28                bool fileexists = false;
 29
 30                try
 31                {
 32                    // 为 applicationsettings 类型创建 xmlserializer。
 33                    myserializer = new xmlserializer(typeof(applicationsettings));
 34                    fileinfo fi = new fileinfo(application.localuserappdatapath
 35                       + @"/myapplication.config");
 36                    // 如果配置文件存在,将其打开。
 37                    if (fi.exists)
 38                    {
 39                        myfilestream = fi.openread();
 40                        // 反序列化配置文件以创建新的
 41                        // applicationsettings 实例。
 42                        applicationsettings myappsettings =
 43                          (applicationsettings)myserializer.deserialize(
 44                           myfilestream);
 45                        // 为 applicationsettings 类的这一实例
 46                        // 分配属性值。
 47                        this.formlocation = myappsettings.formlocation;
 48                        fileexists = true;
 49                    }
 50                }
 51                catch (exception ex)
 52                {
 53                    messagebox.show(ex.message);
 54                }
 55                finally
 56                {
 57                    // 如果 filestream 是打开的,将其关闭。
 58                    if (myfilestream != null)
 59                    {
 60                        myfilestream.close();
 61                    }
 62                }
 63
 64           
 65                return fileexists;
 66            }
 67
 68            // 如果设置发生变化,则将
 69            // 类序列化到配置文件中。
 70            public bool saveappsettings()
 71            {
 72                if (this.appsettingschanged)
 73                {
 74                    streamwriter mywriter = null;
 75                    xmlserializer myserializer = null;
 76                    try
 77                    {
 78                        // 为 applicationsettings 类型
 79                        // 创建 xmlserializer。
 80                        myserializer = new xmlserializer(
 81                          typeof(applicationsettings));
 82                        mywriter =
 83                          new streamwriter(application.localuserappdatapath
 84                          + @"/myapplication.config", false);
 85                        // 将 applicationsettings 类的这一实例
 86                        // 序列化到配置文件中。
 87                        myserializer.serialize(mywriter, this);
 88                    }
 89                    catch (exception ex)
 90                    {
 91                        messagebox.show(ex.message);
 92                    }
 93                    finally
 94                    {
 95                        // 如果 filestream 是打开的,将其关闭。
 96                        if (mywriter != null)
 97                        {
 98                            mywriter.close();
 99                        }
100                    }
101                }
102                return appsettingschanged;
103            }
104        }

出处:ting blog

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表