如何遍历configurationSection

bom*_*mbo 5 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>
Run Code Online (Sandbox Code Playgroud)

我需要编写循环遍历cities给定的代码id并返回type.

 if the given id = "ny" --> return nameSpace1
 if the given id = "dc" --> return nameSpace2
 if the given id = "nj" --> return nameSpace3
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 1

您需要参考以下部分:

var theSection = (TypeOfSection)ConfigurationManager.GetSection("mySection");
Run Code Online (Sandbox Code Playgroud)

请注意对TypeOfSection- 这是配置文件中声明的类型。

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