Reputation: 1075
This question appears to already be answered but the solutions do not work for me. Here's the previous question: Can you have a generic List(of T) in your settings file?
What I'm trying to do is store a generic list of a structure inside my app.config. (ie. I have a structure defined with two fields and I want to store a list of them inside my app.config)
Is this possible? The questioned linked above has several answers but they do not seem to work...
Upvotes: 0
Views: 644
Reputation: 433
The cofig entry seems to be have changed while copy paste here is the one
<?xml version="1.0" encoding="utf-16"?><ArrayOfEmpName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><EmpName><FirstName>Bill</FirstName><LastName>Gates</LastName></EmpName><EmpName><FirstName>Subhash</FirstName><LastName>Lama</LastName></EmpName></ArrayOfEmpName>
Upvotes: 0
Reputation: 433
Yes it is possible e.g
put the following code in the app.confing file under settings key 'Properties.Settings.Default.ConfigName'
BillGatesSubhashLama
I used the following structure one can use a class too: public struct EmpName { public string FirstName { get; set; } public string LastName { get; set; } }
Not the final step to get the value in the list:
XmlSerializer se = new XmlSerializer(typeof(List<EmpName>));
TextReader tr = new StringReader(Properties.Settings.Default.ConfigName);
XmlReader sr = XmlReader.Create(tr);
List<EmpName> names = (List<EmpName>)se.Deserialize(sr);
Please vote me if you get your answer
Upvotes: 0
Reputation: 8152
Sure you can...serialize the list, update your AppSetting, and deserialize when you read it back out.
Upvotes: 0
Reputation: 2048
Kind of... Assuming the two fields serialize to string you could create a custom config section and store them directly in the xml. Then it's a matter of writing a custom section handler to manage (de)serializing to and from a List(Of YourCustomTwoFieldClass). Nothing stops you from just serializing the whole thing down to one string and stuffing it in an AppSetting either. Then you can deserialize when needed.
Edit: Looking at your original question, this isn't the right place to store data. It's not conducive to "profile" information at all. I'd sooner maintain my own flat file than dynamically rewrite the app.config all the time. It's best used for "static" application-wide configuration values.
Have you thought about whipping up your own lightweight profile provider? That would give you all kinds of flexibility for how/when/where you're persisting information.
Upvotes: 1