Ext.Net 1.X_读写配置文件

[摘要]

     有N个ERP数据库帐套,需要从XML文件中读取。

加载指定路径的XML

        /// <summary>
        /// 取得帐套列表
        /// </summary>
        private void GetDBList()
        {
            List<object> data = new List<object>();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"d:\db.xml");
            XmlNode xn = xmlDoc.SelectSingleNode("Mydatabase");
            XmlNodeList xnl = xn.ChildNodes;
            string company = string.Empty;
            string connectstring=string.Empty;  
            foreach (XmlNode xnf in xnl)
            {
                XmlElement xe = (XmlElement)xnf;

                company = xe.GetAttribute("dispalyname");
                connectstring = string.Format("Server={0};Database={1};uid={2};pwd={3}", xe.GetAttribute("Server"), xe.GetAttribute("Database"), xe.GetAttribute("uid"), xe.GetAttribute("pwd"));
                data.Add(new { name = company, connectstring = connectstring });
            }
            this.DataBaseStore.DataSource = data;
            this.DataBaseStore.DataBind();

            this.cbxDb.SelectedIndex = 0;
            
        }

用户选择帐套修改.config

 
 
        /// <summary>
        /// 确认帐套修改配置文件连接字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Login_Click(object sender, DirectEventArgs e)
        {
            if (cbxDb.SelectedItem.Value.ToString() != "")
            {

                BaseConfig bc = new BaseConfig("Config/CurrentConnect.config");
                bc.SaveConfig(this.cbxDb.SelectedItem.Value.Trim(),"ConnectString");

                Response.Redirect("Index.aspx");
                
            }
            else 
            {
                X.Msg.Alert("提示","请选择帐套").Show();
            }
           
        }

        /// <summary>
        /// 写入配置文件 key value
        /// </summary>
        /// <param name="ConnenctionString"></param>
        /// <param name="strKey"></param>
        public  void SaveConfig(string ConnenctionString, string strKey)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = HttpContext.Current.Server.MapPath(configPath); 
  
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value == strKey)
                {
                    //对目标元素中的第二个属性赋值
                    att = nodes[i].Attributes["value"];
                    att.Value = ConnenctionString;
                    break;
                }
            }
            //保存上面的修改
            doc.Save(strFileName);

        }
        /// <summary>
        /// 读取配置文件key-value值
        /// </summary>
        /// <param name="ConnenctionString"></param>
        /// <param name="strKey"></param>
        public string GetConfigKeyValue(string strKey)
        {
            XmlDocument doc = new XmlDocument();
            string ConnectString = string.Empty;
            doc.Load(HttpContext.Current.Server.MapPath(configPath));
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");     
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                if (att.Value == strKey)
                {
                    ConnectString = nodes[i].Attributes["value"].Value.ToString();
                }
            }
            return ConnectString;
        }





你可能感兴趣的:(Ext.Net 1.X_读写配置文件)