- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的控制台应用程序中使用外部配置文件。我的 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/
在 FireBaseDp 中检索数据时,可以使用上述方法之一来获取值。我想知道有什么区别(任何优点/缺点)? 最佳答案 确切的行为取决于您提取的数据类型。 如果您要提取字符串值,则两个代码段的行为是相
我在 output1 和 output2 中得到了不同的结果。第一个给了我在注册表中实际看到的值,而后者只给了我默认值。我想念什么? String output1 = Registry.GetValu
我正在使用Kotlin多平台构建iOS框架,除奇怪的问题外,其他一切都运行良好。 来自Kotlin的代码: fun example(): Map { val result = muta
我最近一直在开发一些 JSF 应用程序,并且对 Web 组件 API 的不一致感到不安。 我注意到在服务器端代码中的 JSF 组件对象上调用 .getValue() 或 .getSubmittedVa
我正在开发 JavaFX 表单。该表单应该获取值并将其添加到数据库中。每当我尝试通过 .getValue 获取值时,它都会产生错误 Incompatible Types: Object cannot
我有下面的代码, var data = new Dictionary { { "A1", new TestData { Name = "N1",
返回的错误是: "TypeError: Cannot call method "getValues" of undefined. (line 7, file "Code")" function set
在 Google 表格中,将范围内的数据存储到数组中作为脚本的一部分,然后检查特定列中是否有“Y”,这样我就可以遍历并将这些列存储在新数组中。 我有以下代码,但出现此错误 - “TypeError:无
我正在尝试编写一个通用方法来比较两个对象(我有意引入两种不同的类型。第二个具有与第一个相同的属性。第一个具有更多属性。)。 我想确保属性具有相同的值。以下代码适用于我在对象中拥有的大多数属性,但偶尔会
我写了这门课。这不是原始类的完整描述,为了使我的问题陈述简洁,我给出了所需的内容 template class RB { class Child { members are Child ar
我正在尝试根据我的团队在工作中使用的项目跟踪表来设置电子邮件警报系统。当 K 列中的任务状态更改为“完成”时,我需要它发送电子邮件。我得到了可以在测试表上运行的代码,但是当我将其复制到实时表时, ge
尝试创建获取任何枚举属性值的枚举扩展方法时,我遇到了以下情况: 编译以下代码时,VS 2010 显示错误:“GetValue”方法没有重载需要 1 个参数。注释代码和使用监视工具时 - 该行返回值(见
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想转换一个 Map>所以它变成了Map .如果它只是 Map在 Java8 中很容易; stream().collect(k -> k.getValue().getMyKey(), Entry::g
我目前正在使用 Scene Builder 制作 javafx 场景。我想以特定格式从日期选择器中获取值。只需使用 datePicker.getValue() 即可以 yyyy-mm-dd 形式返回日
问题是我们无法获取仅存在于具有泛型类型的基类中的字段(非泛型)的值。请看下面的代码片段。呼唤 f.GetValue(a) 将抛出异常消息:后期绑定(bind)操作不能在类型为 Type.Contain
为什么会这样: $RegistryKeys = (Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\services\VMSMP\Parameters\
我试图让我的函数在另一张表的表中查找值。 表格的行用数字标记,但列用字符串命名。 这是我的功能代码 Function GetValue(row As String, col As String)
我试图模拟顶级(不是任何部分的一部分)配置值(.NET Core 的 IConfiguration),但徒劳无功。例如,这些都不起作用(使用 NSubstitute,但它与 Moq 或我相信的任何模拟
我得到了一个代码,它从一个名为频率的列表中获取所有最小值。然后它将最小值与总值的百分比放入一个字符串中。要计算百分比,我想调用 minEntryes.getValue()(minEntryes 是 M
我是一名优秀的程序员,十分优秀!