我的 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/