3

I need to use a config file for my application which will save a list of details for each entity: for example-

<person A>
         <name= >
         <age= >
<person B>
         <name=>
         <age= >

is something of this sort possile in the settings class or this requires using the app.config file wihtout the settings class wrapper?

Edit: My application used to have a single entity, now it should support multiple entities and therefore save multiple entities in some config.

user271077
  • 996
  • 1
  • 19
  • 35
  • But why do you want to store it as a .config? Have you considered using an ordinary XML file? – Smur Dec 07 '10 at 23:56
  • Since I want the API of the settings. and I want it to be generated and named with the applicaiton name- i.e. an application configuration file which this is exactly what it is – user271077 Dec 08 '10 at 00:26
  • A rephrase of the question - could a values of the settings file contain a class? – user271077 Dec 08 '10 at 00:28

3 Answers3

1

Yes, you can save any sort of information (strings) in .config file. You'd then use the ConfigurationManager class to access the saved information by using a Key.

But why would you want to save information like this in the .config?

I recommend you use an XML document for this purpose. You can then use the XDocument class to parse it.

Edit:

After reading your comment, I think the app.config file cannot save an object. So you could not save a class object there.

  • I don't see any reason why you couldn't use <![CDATA[ ... ]]> and put an xml-serialized object (or possibly another simple serializer) in there then deserialize it later (unless someone else knows better? – Brad Christie Dec 08 '10 at 00:38
  • @Brad: I didn't know you could do that. So essentially you'd serialize a class and save some voodoo string? –  Dec 08 '10 at 01:04
  • That's the plan, stan. Again, not sure if it _can_, I just don't see why it _wouldn't_ (given what I know about XML and storing abstract data within). You could probably create the object and decorate it with XmlNodeAttibute and XmlElementAttribute, serialize it to a file, then push it in to the .config. Worst-case scenario, you use XmlDocument and go down the tree yourself (bypass ConfigurationManager) and deserialize the results. – Brad Christie Dec 08 '10 at 01:08
1

The config file is not a good place to store dynamic configuration information, it's intended for configuring an app, not maintaining state.

Rather use a separate file, or something like a SQLite database to store dynamic data.

Doobi
  • 4,844
  • 1
  • 22
  • 17
0

Well, you could use Serialization for this purpose. But I still don't see the point of storing it in the .config file. Anyway, you could serialize the object in a XML format and store it in a key that you would add to the .config file, as a string. I guess that would solve it.

If you decide to store it as a simple XML file, use the XMLDocument and XMLNode classes.

Smur
  • 3,075
  • 7
  • 28
  • 46