Can I save an object to the app.config file?

不羁的心 提交于 2019-12-11 03:28:30

问题


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.


回答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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4382783/can-i-save-an-object-to-the-app-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!