Reputation: 2670
I am developing an application in C#, which needs to store a key value pair in settings file. I tried tried to save an arraylist of dictionary in settings file ,but it fails .Here is what i have done:
if (Settings1.Default.arraylst == null)
{
Settings1.Default.arraylst = new System.Collections.ArrayList();
}
Dictionary<string, string> dd = new Dictionary<string, string>();
dd.Add("1", "1");
Settings1.Default.arraylst.Add(dd);
Settings1.Default.Save();
When i restarted the application the arrarylist became null..
Thanks in advance....
Upvotes: 2
Views: 7088
Reputation: 50712
That's because generic dictionary is not serializable for some reasons,try use this one
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>
: Dictionary<TKey, TValue>, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return;
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
which i found here XML Serializable Generic Dictionary
Upvotes: 4
Reputation: 13907
The C# settings files are just XML files, and will only save classes which are serializable, otherwise the data will not be written out when you save. Dictionary is unfortunately not serializable.
One solution would be to build your own wrapper that properly serializes a dictionary. This link may give you an idea of what's involved in this.
Another solution would be to write your key/value pairs out as a long string array, and parse it when you read it back in.
//Define a string[] in your settings
List<string> keyValues = new List<string>();
foreach(KeyValuePair<string,string> pair in dd)
{
keyValues.Add(pair.Key);
keyValues.Add(pair.Value);
}
Settings1.Default.KeyValuePairs = keyValues.ToArray();
Then read back the pairs when you load in a similar fashion.
Upvotes: 1