我的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>
我需要编写在给定
cities
的情况下遍历id
并返回type
的代码。IE。
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/