XML配置文件的读取与修改

XML配置文件的读取与修改

复制代码
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <!--串口数据-->
    <add key ="Port" value ="5"/>
  </appSettings>
  <!--数据库配置-->
  <connectionStrings>
      <add name="SMSSQL" connectionString="Data Source=192.168.0.8;Initial Catalog=SMS;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
复制代码

 

一 . 读取配置文件

1. 用ConfigurationManager来读取

   引用:System.Configuration;

   public static string conString = ConfigurationManager.ConnectionStrings["SMSSQL"].ConnectionString;

 

2. 用XML中的XmlDocument

    引用:System.Xml;

    /// <summary>
        /// 读取数据库配置文件
        /// </summary>
        /// <returns>无返回值</returns>
        public static string readConn()
        {
            try
            {

                private static string configPath = System.Windows.Forms.Application.StartupPath + "\\App.config";
                System.IO.FileInfo FileInfo = new System.IO.FileInfo(configPath);
                //App.config文件是否存在
                if (!FileInfo.Exists)
                {
                    throw new ApplicationException("没有找到web.config配置文件!");
                }
                System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
                xmlDocument.Load(configPath);
                string strConn = "";
                foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
                {
                    if (Node.Name == "add")
                    {
                        if (Node.Attributes.GetNamedItem("name").Value == "SMSSQL")
                        {
                            strConn = Node.Attributes["connectionString"].Value;
                        }
                    }
                }
                return strConn;
            }
            catch (Exception ex)
            {
                
                throw new ApplicationException(ex.Message);
            }

 

二.  修改配置文件内

      private bool UpdateWebConfig()
        {
            string configPath = System.Windows.Forms.Application.StartupPath + "\\App.config";
            System.IO.FileInfo FileInfo = new System.IO.FileInfo(configPath);
            if (!FileInfo.Exists) //不存在web.config文件
            {
                throw new ApplicationException("没有找到web.config配置文件!");
            }
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load(FileInfo.FullName);
            bool FoundIt = false;
            foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
            {
                if (Node.Name == "add")
                {
                    if (Node.Attributes.GetNamedItem("name").Value == "OASQL")
                    {
                        Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Data Source={0};database={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", _DataSource, _DBName, _UserID, _PassWord);
                        FoundIt = true;
                        break;
                    }
                }
            }
            if (!FoundIt)
            {
                throw new ApplicationException("修改web.config配置文件失败!");
            }
            xmlDocument.Save(FileInfo.FullName);
            return FoundIt;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值