How can I serialize an object with a Dictionary property?

前端 未结 7 544
故里飘歌
故里飘歌 2021-01-04 08:04

In the example code below, I get this error:

Element TestSerializeDictionary123.Customer.CustomProperties vom Typ System.Collec

7条回答
  •  星月不相逢
    2021-01-04 08:45

    Try Serializating through BinaryFormatter

    private void Deserialize()
        {
            try
            {
                var f_fileStream = File.OpenRead(@"dictionarySerialized.xml");
                var f_binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                myDictionary = (Dictionary)f_binaryFormatter.Deserialize(f_fileStream);
                f_fileStream.Close();
            }
            catch (Exception ex)
            {
                ;
            }
        }
        private void Serialize()
        {
            try
            {
                var f_fileStream = new FileStream(@"dictionarySerialized.xml", FileMode.Create, FileAccess.Write);
                var f_binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                f_binaryFormatter.Serialize(f_fileStream, myDictionary);
                f_fileStream.Close();
            }
            catch (Exception ex)
            {
                ;
            }
        }
    

提交回复
热议问题