產生XMl設定檔常用方法

來源:互聯網
上載者:User

根據開發需要,做軟體嘗嘗要儲存一些配置資訊,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();//不存在返回一個Null 物件
               }
             
           }
           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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.