在使用企业类库时想取出单独企业配置文件中的连接字符串遍历,并放到自己的数据库处理类中,在查找了很久的资料后都没有找到,于是自己探索着写了一个,共享给大家以做参考:
ConfigurationSourceSection sec = (ConfigurationSourceSection)ConfigurationManager.GetSection("enterpriseLibrary.ConfigurationSource"); PolymorphicConfigurationElementCollection<ConfigurationSourceElement> po = (PolymorphicConfigurationElementCollection<ConfigurationSourceElement>)sec.Sources; FileConfigurationSourceElement ele = (FileConfigurationSourceElement)po.Get(0); string filepath = ele.FilePath; ConfigXmlDocument dc = new ConfigXmlDocument(); string path = AppDomain.CurrentDomain.BaseDirectory + filepath; dc.Load(path); foreach (XmlNode item in dc.SelectNodes("configuration/connectionStrings/add")) { string name = item.Attributes["name"].Value; string connectionString = item.Attributes["connectionString"].Value; }
其中的“enterpriseLibrary.ConfigurationSource”为企业类库单独配置节的Section