John S
John S

Reputation: 8331

Storing a variable collection of strings in Web.Config?

I would like to store a collection of strings in the web.config. This collection would vary in size over time. I would like to be able to pull all of the strings in the collection into an array or collection in code. (.Net 4, asp.net) i.e.

<customCodes>
<VendorCode vendorName="Name1" code="1234567891234567891324567987ddd" isActive="true"/>
<VendorCode vendorName="Name2" code="1sadfsadf1234567891324567987ddd"  isActive="true" />
<VendorCode vendorName="Name3" code="123456789dfadfdsaf3324567987d32"  isActive="true"/>
</customCodes>

I could use appsettings with the strings all in one value but I would like to seperate it out for organizational reasons.

Any code snippets or examples would be appreciated.

Sorry this was posted without the code example the first time. Not using the key/value pair complicates things a bit. I am now getting a message that states "you can't have duplicate elements in a section"

TIA J

Upvotes: 0

Views: 3294

Answers (2)

John S
John S

Reputation: 8331

The link http://dotnetslackers.com/articles/customconfiguration/Custom_Configuration_Collections.aspx was very helpful in accomplishing what I wanted to do. I did have to settle for using the "Add" tags instead of the custom tags that I origianally wanted.

Thanks for the help Dirk.

J

Upvotes: 0

Dirk Brockhaus
Dirk Brockhaus

Reputation: 5062

You can do this by creating a custom configuration section handler:

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

For collections have a look at the ConfigurationElementCollection

http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx

Following the second link a little deeper there is the code you need to to solve your problem. Just change somes tags and use "Add" instead of "VendorCode" :-)

http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

Upvotes: 2

Related Questions