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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With