生成XMl配置文件常用方法

根据开发需要,做软件尝尝要保存一些配置信息,web.Config虽然提供了appSettings来实现保存一些简单的配置的功能,但是有些时候并不能随心所欲根据业务需要执行更多扩展的处理,这里总结了一些采用XMl做配置保存。

 例如我们来保存数据库的配置信息XMl文件如下:

 1.  <?xml version="1.0"?>
<Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Username>金培龙</Username>
  <Password>sa</Password>
  <Server>中国百方网</Server>
  <Database>sa</Database>
</Model>

 

2.XMl配置操作类  [XmlElement]表示为了更好的实现序列化

     public class Model
    {
        private string server;
        private string username;  
        private string password;
        private string database;

         [XmlElement]
        public string Username
        {
            get { return username; }
            set { username = value; }
        }

         [XmlElement]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

         [XmlElement]
        public string Server
        {
            get { return server; }
            set { server = value; }
        }
         [XmlElement]
        public string Database
        {
            get { return database; }
            set { database = value; }
        }
    }

3.在添加一个配置项的操作类

  public class ModelConfig
    {
       //获取文件路径
       public static string GetSettingsFile()
       {
           HttpContext context = HttpContext.Current;
           //把路径存放进CaChe里面
           string filePath =(string)context.Cache["ModelSettingsFiles"];
           if (filePath == null)
           {
               filePath = ConfigurationManager.AppSettings["ModelSettingsFiles"];
               //把路径存入缓存中
               context.Cache["ModelSettingsFiles"] = filePath;
           }
           return filePath;
       }

       //保存文件
       public static void SaveFileInfo(Model data)
       {
           //服务器端获取路径的方法
          string filePath=HttpContext.Current.Server.MapPath(GetSettingsFile());
          XmlSerializer serializer = new XmlSerializer(typeof(Model));
          FileStream fs = new FileStream(filePath, FileMode.Create);
          serializer.Serialize(fs, data);
          fs.Close();
       }

       //获取文件(反序列化之后的文件)
       public static Model GetAllFileInfo()
       {
           HttpContext current = HttpContext.Current;
           //把反序列化的内容放入Cache中
           Model data=(Model)current.Cache["Mode"];
           if (data == null)
           {
               XmlSerializer serialzer = new XmlSerializer(typeof(Model));
               try
               {
                   string filePath = HttpContext.Current.Server.MapPath(GetSettingsFile());
                   FileStream fs = new FileStream(filePath, FileMode.Open);
                   data = (Model)serialzer.Deserialize(fs);
                   fs.Close();
                   current.Cache.Insert("Mode", data, new CacheDependency(filePath));
               }
               catch (System.IO.FileNotFoundException)
               {

                   data = new Model();//不存在返回一个空对象
               }
             
           }
           return data;
       }
    }
}

4.在前台调用生成xml配置文件

 protected void btnSave_Click(object sender, EventArgs e)
    {
        Model setting = new Model();
        setting.Server = txtAddress.Text.Trim();
        setting.Username = txtUserName.Text.Trim();
        setting.Password = txtPassword.Text.Trim();
        setting.Database = txtDataBase.Text.Trim();
        ModelConfig.SaveFileInfo(setting);
        Response.Write("OK");
    }

5.获取xml配置信息

    protected void btnRead_Click(object sender, EventArgs e)
    {
        Model setting = ModelConfig.GetAllFileInfo();
         this.lblAddress.Text = setting.Server;
         this.lblUserName.Text = setting.Username;
         this.lblPassword.Text = setting.Password;
         this.lblDataBase.Text = setting.Database;

    }

s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值