Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialize Class containing Dictionary member
    primarykey
    data
    text
    <p>Expanding upon my <a href="https://stackoverflow.com/questions/489173/writing-xml-with-c">earlier problem</a>, I've decided to (de)serialize my config file class which worked great. </p> <p>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 <code>Dictionary</code>, <code>HybridDictionary</code>, and <code>Hashtable</code> for this but I always get the following error when calling <code>ConfigFile.Load()</code> or <code>ConfigFile.Save()</code>:</p> <blockquote> <p>There was an error reflecting type 'App.ConfigFile'. [snip] System.NotSupportedException: Cannot serialize member App.Configfile.mappedDrives [snip]</p> </blockquote> <p>From what I've read Dictionaries and HashTables can be serialized, so what am I doing wrong?</p> <pre><code>[XmlRoot(ElementName="Config")] public class ConfigFile { public String guiPath { get; set; } public string configPath { get; set; } public Dictionary&lt;string, string&gt; mappedDrives = new Dictionary&lt;string, string&gt;(); 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(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload