c# save dictionary with <string,List<string>> in .dat with Binaryformatter (or something else)

206 views Asked by At
     FileStream fs = new FileStream("Answears.dat", FileMode.Create);
     Dictionary<string, List<string>> chats = new Dictionary<string, List<string>>();

     BinaryFormatter formatter = new BinaryFormatter();
     public void Start()
    {

                chats = (Dictionary<string, List<String>>) formatter.Deserialize(fs); //here is a error 
                fs.Close();

    }

do you now any other option to save the dictionary(and also later load again) in a file. ^^

thanks for you help

Paul :)

1

There are 1 answers

0
L.B On

I would use Json.Net, independent from assembly version changes and results in a readable text...

File.WriteAllText(filename, JsonConvert.SerializeObject(yourDict));

Later, you can load it as

var yourDict = JsonConvert.DeserializeObject<Dictionary<string,List<string>>>(File.ReadAllText(filename));