c# - 如何遍历 configurationSection

标签 c# configurationsection

我的 Web.config 文件中有以下部分:

<configSections>
    <section name="mySection" type="myNameSpace, myProject"/>
</configSections>


<mySection>
    <city id="ny" type="nameSpace1" />
    <city id="dc" type="nameSpace2" />
    <city id="nj" type="nameSpace3" />
</mySection>

我需要编写代码,在给定 id 的情况下遍历 cities 并返回 type

 if the given id = "ny" --> return nameSpace1
 if the given id = "dc" --> return nameSpace2
 if the given id = "nj" --> return nameSpace3

最佳答案

您需要引用该部分:

var theSection = (TypeOfSection)ConfigurationManager.GetSection("mySection");

注意 TypeOfSection 的转换 - 这是在配置文件中声明的类型。

此时,您应该有一个可以访问和迭代的强类型对象。

关于c# - 如何遍历 configurationSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13361734/

相关文章:

.net - 与 ConfigurationProperty 属性一起使用时,属性类型的隐式约定是什么?

c# - 我可以在自定义 ConfigurationSection 上使用 IntegerValidator 属性指定范围吗?

c# - 为什么实现带有类型约束的泛型接口(interface)的泛型类需要重复这些约束?

.net - 在运行时创建或编辑自定义配置部分

c# - 为什么 ConfigurationSection 需要使用字符串查找内容?

c# - 如何从POSTMAN调用WCF服务方法

c# - 你如何使用 c# 4.0 app.config 中的部分?

c# - 为什么我需要用这个简单的 LINQ 表达式来引用 System.Numerics?

c# - 方法之间的歧义

c# - SQL查询执行难度