App.config 在 .NET 中格式化和访问数据
App.config format and accessing data in .NET
我是编辑新手 app.config。我想在 app.config:
中包含此部分
<name1>
<name2>
<name3 att="something1">value1</name3>
<name3 att="something2">value2</name3>
...
</name2>
</name1>
如何创建它并从代码中访问它?我想得到 something1
、something2
、value1
和 value2
。
我找到了 this tutorial,但它只显示了如何获得 something1
,而不是 something2
、value1
和 value2
(教程中的方法四)。
感谢您的帮助。
您需要通过子类化 ConfigurationElementCollection
来自定义配置集合。
假设 <name1>
是您需要的根元素的直接子元素:
<name1>
的自定义部分
<name2>
需要一个自定义集合
- 每个
<name3>
. 的自定义元素(成为集合的成员)
我解决了。首先,我需要为 <name3>
编辑 ConfigurationElement
class,我在这个 post. Then I needed to create class from ConfigurationElementCollection 中使用了示例 - 所以 UrlsCollection
class 的简单复制代码来自link 并将所有 UrlConfigElement
覆盖到我的 ConfigurationElement
class.
var section = ConfigurationManager.GetSection("name1") as Name1ConfigurationElement;
var collection = section.Name2;
foreach (SqlElement element in collection)
{
Console.WriteLine(element.Value);
}
我是编辑新手 app.config。我想在 app.config:
中包含此部分<name1>
<name2>
<name3 att="something1">value1</name3>
<name3 att="something2">value2</name3>
...
</name2>
</name1>
如何创建它并从代码中访问它?我想得到 something1
、something2
、value1
和 value2
。
我找到了 this tutorial,但它只显示了如何获得 something1
,而不是 something2
、value1
和 value2
(教程中的方法四)。
感谢您的帮助。
您需要通过子类化 ConfigurationElementCollection
来自定义配置集合。
假设 <name1>
是您需要的根元素的直接子元素:
<name1>
的自定义部分
<name2>
需要一个自定义集合
- 每个
<name3>
. 的自定义元素(成为集合的成员)
我解决了。首先,我需要为 <name3>
编辑 ConfigurationElement
class,我在这个 post. Then I needed to create class from ConfigurationElementCollection 中使用了示例 - 所以 UrlsCollection
class 的简单复制代码来自link 并将所有 UrlConfigElement
覆盖到我的 ConfigurationElement
class.
var section = ConfigurationManager.GetSection("name1") as Name1ConfigurationElement;
var collection = section.Name2;
foreach (SqlElement element in collection)
{
Console.WriteLine(element.Value);
}