读取任一web.config的配置信息

 public static readonly DbSession Context = null;

        static DB()
        {
            try
            {
                string rootPath = AppDomain.CurrentDomain.BaseDirectory;
                string webconfigPath = rootPath + "Web.config";//配置文件路径
                ConfigXmlDocument document = new ConfigXmlDocument();
                document.Load(webconfigPath);
                System.Xml.XmlNodeList list = document.GetElementsByTagName("activerecord");
                string configXml = list[0].InnerXml;
                document.LoadXml(configXml);
                document.LoadXml(document.InnerXml);//activerecord>config节
                System.Xml.XmlElement xml = document.DocumentElement;
                System.Xml.XmlNodeList nodelist = xml.ChildNodes;
                string connectionString = string.Empty;//数据库连接字符串
                foreach (System.Xml.XmlNode item in nodelist)
                {
                    if (item.Name.Equals("#comment"))
                    {
                        continue;
                    }
                    if (item.Attributes != null)
                    {
                        string key = item.Attributes["key"].Value;
                        string value = item.Attributes["value"].Value;
                        if (key.Equals("connection.connection_string"))
                        {
                            connectionString = value;
                            break;
                        }
                    }
                }
                Context = new DbSession(DatabaseType.Oracle, connectionString);              
            }
            catch (Exception ex)
            {
                string errmsg = "";
                if (ex.InnerException != null)
                {
                    errmsg = ex.InnerException.Message;
                }
                else
                {
                    errmsg = ex.Message;
                }             
            }
        }




本文转载:CSDN博客