Expanding upon my earlier problem, I've decided to (de)serialize my config file class which worked great.
I now want to store an associative array of drive letters to map (key is the drive letter, value is the network path) and have tried using Dictionary
, HybridDictionary
, and Hashtable
for this but I always get the following error when calling ConfigFile.Load()
or ConfigFile.Save()
:
There was an error reflecting type 'App.ConfigFile'. [snip] System.NotSupportedException: Cannot serialize member App.Configfile.mappedDrives [snip]
From what I've read Dictionaries and HashTables can be serialized, so what am I doing wrong?
[XmlRoot(ElementName="Config")]
public class ConfigFile
{
public String guiPath { get; set; }
public string configPath { get; set; }
public Dictionary<string, string> mappedDrives = new Dictionary<string, string>();
public Boolean Save(String filename)
{
using(var filestream = File.Open(filename, FileMode.OpenOrCreate,FileAccess.ReadWrite))
{
try
{
var serializer = new XmlSerializer(typeof(ConfigFile));
serializer.Serialize(filestream, this);
return true;
} catch(Exception e) {
MessageBox.Show(e.Message);
return false;
}
}
}
public void addDrive(string drvLetter, string path)
{
this.mappedDrives.Add(drvLetter, path);
}
public static ConfigFile Load(string filename)
{
using (var filestream = File.Open(filename, FileMode.Open, FileAccess.Read))
{
try
{
var serializer = new XmlSerializer(typeof(ConfigFile));
return (ConfigFile)serializer.Deserialize(filestream);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.ToString());
return new ConfigFile();
}
}
}
}
the Dictionary class implements ISerializable. The definition of Class Dictionary given below.
I don't think that is the problem. refer to the below link, which says that if you are having any other data type which is not serializable then Dictionary will not be serialized. http://forums.asp.net/t/1734187.aspx?Is+Dictionary+serializable+
Create a serialization surrogate.
Example, you have a class with public property of type Dictionary.
To support Xml serialization of this type, create a generic key-value class:
Add an XmlIgnore attribute to your original property:
Expose a public property of array type, that holds an array of SerializableKeyValue instances, which are used to serialize and deserialize into the SearchCategories property:
Instead of using
XmlSerializer
you can use aSystem.Runtime.Serialization.DataContractSerializer
. This can serialize dictionaries and interfaces no sweat.Here is a link to a full example, http://theburningmonk.com/2010/05/net-tips-xml-serialize-or-deserialize-dictionary-in-csharp/
This article explains exactly how to handle this: How do I... Serialize a hash table in C# when the application requires it?
I hope this is helpful