2013-05-13 83 views
4

我需要从app/web.config中的自定义节中读取键值。在C#中读取自定义配置节的键值

我通过

Reading a key from the Web.Config using ConfigurationManager

How can I retrieve list of custom configuration sections in the .config file using C#?

然而,他们并不指定如何读取自定义栏目时,我们需要明确指定路径到配置文件(在我的情况下,配置文件不在它的默认位置)

我的web.config文件的示例:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <MyCustomTag> 
    <add key="key1" value="value1" /> 
    <add key="key2" value="value2" /> 
    </MyCustomTag> 
<system.web> 
    <compilation related data /> 
</system.web> 
</configuration> 

其中我需要读取MyCustomTag中的键值对。

当我尝试(configFilePath是通向我的配置文件): -

var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath }; 

var config = 
      ConfigurationManager.OpenMappedExeConfiguration(
      configFileMap, ConfigurationUserLevel.None); 

     ConfigurationSection section = config.GetSection(sectionName); 

     return section[keyName].Value; 

我得到一个错误,说明在部分[的keyName]

+1

请加入你的web.config(全部或部分)。 – 2013-05-13 14:35:01

+0

与web.config中的自定义设置的路径无关。你将使用'' – Saravanan 2013-05-13 14:38:00

+0

@FrancescoDeLisi Question edited。 – Karan 2013-05-14 05:01:49

回答

7
“无法访问受保护的内部索引‘这个’这里”

不幸的是,这听起来并不容易。解决该问题的方法是使用ConfigurationManager获取文件配置文件,然后使用原始xml。所以,我通常使用以下方法:

private NameValueCollection GetNameValueCollectionSection(string section, string filePath) 
{ 
     string file = filePath; 
     System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); 
     NameValueCollection nameValueColl = new NameValueCollection(); 

     System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 
     map.ExeConfigFilename = file; 
     Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
     string xml = config.GetSection(section).SectionInformation.GetRawXml(); 
     xDoc.LoadXml(xml); 

     System.Xml.XmlNode xList = xDoc.ChildNodes[0]; 
     foreach (System.Xml.XmlNode xNodo in xList) 
     { 
      nameValueColl.Add(xNodo.Attributes[0].Value, xNodo.Attributes[1].Value); 

     } 

     return nameValueColl; 
} 

,并且该方法的调用:

var bla = GetNameValueCollectionSection("MyCustomTag", @".\XMLFile1.xml"); 


for (int i = 0; i < bla.Count; i++) 
{ 
    Console.WriteLine(bla[i] + " = " + bla.Keys[i]); 
} 

结果:

enter image description here

+1

这个答案没有提到为什么这种方法比定义一个自定义的section类更好(它对于简单的情况来说更直接,但仍然)。 – 2013-05-14 05:40:24

+0

@凯谢谢。我已经接受你的答案。 @“。\ XMLFile1.xml”是配置文件的路径,对吗? – Karan 2013-05-14 05:45:22

+0

@Kai It works!另外,我用字典替换了NameValueCollection以获得更好的性能。 – Karan 2013-05-14 06:58:37

0

我已经搜查了很多寻找解决方案为同样的问题。经过一些搜索和调试后,我在我的应用程序中使用了它。我希望它也能帮助你。

https://synvistech.wordpress.com/2014/02/12/how-to-implement-custom-configsection-in-your-configuration/

+1

请注意,[只有链接的答案](http://meta.stackoverflow.com/tags/link-only-answers/info)不鼓励,所以答案应该是搜索解决方案的终点(vs.而另一个引用的中途停留时间往往会随着时间推移而过时)。请考虑在此添加独立的摘要,并将链接保留为参考。 – kleopatra 2014-02-12 10:39:51

+0

链接现在消失了。这里有一个网页存档链接:https://web.archive.org/web/20150423202332/http://synvistech.com/blogs/how-to-implement-custom-configsection-in-your-configuration – derFunk 2015-12-15 15:53:37

+0

链接已更新。 – 2016-02-03 09:39:26

相关问题