Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through configurationSection

I have the following section in my Web.config file:

<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>

I need to write code that loops through the cities given the id and return the type.

i.e.

 if the given id = "ny" --> return nameSpace1
 if the given id = "dc" --> return nameSpace2
 if the given id = "nj" --> return nameSpace3
like image 840
bombo Avatar asked Nov 12 '22 17:11

bombo


1 Answers

You need a reference to the section:

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

Note the cast to the TypeOfSection - this is the type declared in the config file.

At this point, you should have a strongly typed object that you can access and iterate over.

like image 200
Oded Avatar answered Nov 15 '22 10:11

Oded