[摘要]

有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_读写配置文件的更多相关文章

  1. 用ConfigParser模块读写配置文件——Python

    对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...

  2. 使用ConfigurationManager类读写配置文件

    使用ConfigurationManager类 读写配置文件app.config,以下为代码: view plaincopy to clipboard print? using System; usi ...

  3. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

  4. C读写配置文件

    在项目开发中,经常需要读取应用配置文件的初始化参数,用于应用在启动前进行一些初始化配置.比如:Eclipse,参数项包含主题.字体大小.颜色.Jdk安装位置.自动提示等.Eclispe配置的文件格式是 ...

  5. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  6. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  7. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  8. python-ConfigParser模块【读写配置文件】

    对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...

  9. python 读写配置文件

    使用python读写配置文件,写个demo测试一下. #!/usr/bin/env python import os import ConfigParser # 如果文件不存在,就穿件文件. if o ...

随机推荐

  1. PHP 安全 E-mail

    PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> <body> <?php if (isset($_REQUEST['email'])) / ...

  2. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

  3. JFinal中使用QuartzPlugin报ClassCastException解决方法

    JDK1.8中泛型反射修改对旧版本的影响 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 问题复现环境: JDK1.8 JFinal1.9 quart ...

  4. 国内外主流BI工具介绍和点评

    商业智能的应用在国外已广为普及,并且开始不断探索大数据和云技术.而国内,商业智能BI工具在这几年才开始慢慢被接受,企业开始有意识地建立一体化数据分析平台,为经营决策提供分析. 从国内企业使用情况来看, ...

  5. RxJava(四) concatMap操作符用法详解

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51533282 本文出自:[余志强的博客] concatMap操作符的 ...

  6. SpriteKit:在场景过渡中暂停动画

    Pausing Scenes During a Transition 你应该意识到两个重要的SKTrnsition属性在场景之间的过渡中. 它们是pausesIncomingScene和pausesO ...

  7. android的消息通知栏

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  8. Java基本语法-----java二维数组

    由于word里的样式在csdn上调太麻烦了,所以我再次贴图了,后面二维数组那里是文字的,大家将就看吧. 二维数组常见的操作: 1.遍历二维数组 2.对二维数组求和 class Demo { // 定义 ...

  9. JAVA面向对象-----java面向对象的六大原则

    现在编程的主流语言基本上都是面向对象的.如C#,C++,JAVA.我们在使用时,已经构造了一个个的类.但是往往由于我们在类内部或外部的设计上存在种 种问题,导致尽管是面向对象的语言,却是面向过程的逻辑 ...

  10. Struts 2 之资源国际化

    首先在struts.properties文件中加入以下内容: struts.custom.i18n.resources=messageResource  或在struts.xml中加入 <con ...