gpt4 book ai didi

c# - NameValueCollection 的 GetValues 不返回具有重复项的键的所有值

转载 作者:行者123 更新时间:2023-11-30 23:17:17 25 4
gpt4 key购买 nike

我正在尝试在我的控制台应用程序中使用外部配置文件。我的 App.config 看起来像这样。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="MyConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<section name="MyConfig2" type="System.Configuration.NameValueFileSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<section name="MyConfig3" type="System.Configuration.NameValueFileSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<MyConfig configSource="MyCustomConfig.config"/>
<MyConfig2 configSource="MyCustomConfig2.config"/>
<MyConfig3 configSource="MyCustomConfig3.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
</configuration>

在配置文件中,我有几个预期的重复键。

<?xml version="1.0" encoding="utf-8" ?>
<MyConfig>
<add key="NodeName" value="node1"/>
<add key="NodeName" value="node2"/>
<add key="NodeName" value="node3"/>
<add key="NodeName" value="node4"/>
<add key="NodeName" value="node5"/>
<add key="NodeProperty1" value="value1"/>
<add key="NodeProperty2" value="value2"/>
<add key="NodeProperty3" value="value3"/>
</MyConfig>

我正在尝试使用以下代码读取 NodeName 的值。

var ConfigData = (NameValueCollection)ConfigurationManager.GetSection("MyConfig");
foreach (string Nodename in ConfigData.GetValues("NodeName"))
{
Console.WriteLine("Node Name:" + Nodename);
}

使用上面的代码,我只获取 node5 而不是获取 node1 到 node5 的值。尽管 GetValues 应该返回一个字符串数组,但该数组本身只有 1 个值。请让我知道需要更正的内容,以便我可以读取所有值。

我知道我可以使用逗号分隔值实现所需的结果,但节点名的数量可以增长到很大的数量是 future 的事。所以我不希望有人设置配置在将新节点值添加为 CSV 时出现任何错误。因此,我想寻求一个选项,允许我将它们设置为单独的键值对。

最佳答案

当前没有支持您的方案的内置配置类。您必须自己构建。

我创建了这个实现,它将每个键的值作为 List<string> 返回.

public class NameValuesSection : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
var nameValues = new NameValuesCollection();
foreach (XmlNode xmlNode in section.ChildNodes)
{
switch(xmlNode.Name)
{
case "add":
List<string> values;
string key = xmlNode.Attributes["key"].Value;
string value = xmlNode.Attributes["value"].Value;
// see if we already had this key
if (nameValues.TryGetValue(
key,
out values))
{
// yep, let's add another value to the list
values.Add(value);
}
else
{
// nope, let's create the list and add it to the dictionary
values = new List<string>(new string[] { value });
nameValues.Add(key, values);
}
break;
default:
// only add is supported now, not remove and clear
throw new ArgumentException("is not a valid node ", xmlNode.Name);
}
}
return nameValues;
}
}

返回的类型是通用字典的子类:

public class NameValuesCollection : Dictionary<string, List<string>>
{
// add helper methods if needed
}

当您想扩展访问值的各种方式时,这可能很有用,例如通过提供获取第一项或最后一项的选项。

在你的配置文件中你必须调整你的configsection :

<section name="MyConfig1" type="ConsoleApplication1.NameValuesSection, ConsoleApplication1"/>

类型是从 [namespace].[classname] 和您的 NameValuesSection 编译到的程序集中的逗号之后构建的。

这是您如何使用该新部分:

var data2 = ConfigurationManager.GetSection("MyConfig1");

var nvc = (NameValuesCollection)data2;
foreach (var keyname in nvc.Keys)
{
Console.Write("Node Name: {0} = [" , keyname);
// loop over all values in the List
foreach(var val in nvc[keyname])
{
Console.Write("{0};", val);
}
Console.WriteLine("]");
}

这将是输出:

Node Name: NodeName = [node1;node2;node3;node4;node5;]
Node Name: NodeProperty1 = [value1;]
Node Name: NodeProperty2 = [value2;]
Node Name: NodeProperty3 = [value3;]

关于c# - NameValueCollection 的 GetValues 不返回具有重复项的键的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41528104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com