Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取配置节的通用方法_C#_.net_Version Control - Fatal编程技术网

C# 读取配置节的通用方法

C# 读取配置节的通用方法,c#,.net,version-control,C#,.net,Version Control,我试图实现一种从配置文件中读取节的通用方法。配置文件可能包含“标准”部分或“自定义”部分,如下所示 <configuration> <configSections> <section name="NoteSettings" type="System.Configuration.NameValueSectionHandler"/> </configSections> <appSettings> <add ke

我试图实现一种从配置文件中读取节的通用方法。配置文件可能包含“标准”部分或“自定义”部分,如下所示

<configuration> 
<configSections>
    <section name="NoteSettings" type="System.Configuration.NameValueSectionHandler"/>
</configSections>   
<appSettings>
    <add key="AutoStart" value="true"/>
    <add key="Font" value="Verdana"/>
</appSettings>  
<NoteSettings>
    <add key="Height" value="100"/>
    <add key="Width" value="200"/>
</NoteSettings> 
假设所有自定义节都只有键值

  • 这样的实施可能吗?如果是的话,有没有比这个更“干净”更优雅的解决方案
  • 上述方法还读取“不可见”部分,如mscorlib、system.diagnostics。这是可以避免的吗
  • System.Data.Dataset返回无法转换为NameValueCollection的数据集。如何处理呢
欢迎更正/建议


谢谢。

将配置读入XML文档,然后使用XPath查找要查找的元素

有点像

XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("~/web.config"));

XmlNodeList list = doc.SelectNodes("//configuration/appSettings");

foreach (XmlNode node in list[0].ChildNodes)

由于配置文件是XML文件,因此可以使用XPath查询执行此任务:

    Configuration configFile = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
    XmlDocument document = new XmlDocument();
    document.Load(configFile.FilePath);
    foreach (XmlNode node in document.SelectNodes("//add"))
    {
        string key = node.SelectSingleNode("@key").Value;
        string value = node.SelectSingleNode("@value").Value;
        Console.WriteLine("{0} = {1}", key, value);
    }
如果需要获取所有{key,value}对,则需要定义XPath查询的三元组:
1-选择结构相似节点的主查询。2,3-用于从第一次查询检索到的节点中提取关键节点和值节点的查询。在您的情况下,对所有节点进行公共查询就足够了,但是维护对不同自定义节的支持很容易。

当您指定
NameValueSectionHandler
作为节的type属性并调用
Configuration.GetSection(string)
时,您将收到一个
DefaultSection
实例作为返回类型

string SectionInformation.SectionInformation.GetRawXml()
是本例中进入数据的关键

我使用
System.Configuration
回答了另一个类似的问题,您可以参考它来获取所有详细信息和代码片段。

您可以阅读如下自定义部分:

var sectionInformation = configuration.GetSection("mysection").SectionInformation;
var xml = sectionInformation.GetRawXml();
var doc = new XmlDocument();
doc.LoadXml(xml);
IConfigurationSectionHandler handler = (IConfigurationSectionHandler)Type.GetType(sectionInformation.Type).GetConstructor(new Type[0]).Invoke(new object[0]);
var result = handler.Create(null, null, doc.DocumentElement);
var sectionInformation = configuration.GetSection("mysection").SectionInformation;
var xml = sectionInformation.GetRawXml();
var doc = new XmlDocument();
doc.LoadXml(xml);
IConfigurationSectionHandler handler = (IConfigurationSectionHandler)Type.GetType(sectionInformation.Type).GetConstructor(new Type[0]).Invoke(new object[0]);
var result = handler.Create(null, null, doc.DocumentElement);