出现FormatException与IsolatedStorageSettings(FormatEx

2019-10-17 10:14发布

序列化时,我有一个问题Dictionary<string,Person>到IsolatedStorageSettings。 我做的是以下情况:

public Dictionary<string, Person> Names = new Dictionary<string, Person>();

if (!IsolatedStorageSettings.ApplicationSettings.Contains("Names"))
        {
            //Add to dictionary
            Names.Add("key", new Person(false,
                new System.Device.Location.GeoCoordinate(0, 0),
                new List<GeoCoordinate>() 
                {
                    new GeoCoordinate(35.8974, 14.5099),
                    new GeoCoordinate(35.8974, 14.5099),
                    new GeoCoordinate(35.8973, 14.5100),
                    new GeoCoordinate(35.8973, 14.5099)
                }));
            //Serialize dictionary to IsolatedStorage
            IsolatedStorageSettings.ApplicationSettings.Add("Names", Names);
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

这里是我的Person类:

[DataContract]
public class Person
{
    [DataMember]
    public bool Unlocked { get; set; }
    [DataMember]
    public GeoCoordinate Location { get; set; }
    [DataMember]
    public List<GeoCoordinate> Bounds { get; set; }

    public Person(bool unlocked, GeoCoordinate location, List<GeoCoordinate> bounds)
    {
        this.Unlocked = unlocked;
        this.Location = location;
        this.Bounds = bounds;
    }
}

代码工作第一次,但在第二轮我得到一个System.FormatException在if条件。 任何帮助,将不胜感激感谢。

PS:我尝试过的IsolatedStorageSettings.ApplicationSettings.Clear(),但调用Clear也给出了FormatException

我发现一些新的东西...发生异常时25倍,或至少这是多少次在输出窗口中显示出来。 然而在此之后,数据是完全反序列化。 我应该担心的异常,如果他们不停止程序的执行?

编辑:这是调用堆栈发生异常时:

mscorlib.dll!double.Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider) + 0x17 bytes  
System.Xml.dll!System.Xml.XmlConvert.ToDouble(string s) + 0x4b bytes    
System.Xml.dll!System.Xml.XmlReader.ReadContentAsDouble() + 0x1f bytes  
System.Runtime.Serialization.dll!System.Xml.XmlDictionaryReader.XmlWrappedReader.ReadContentAsDouble() + 0xb bytes  
System.Runtime.Serialization.dll!System.Xml.XmlDictionaryReader.ReadElementContentAsDouble() + 0x35 bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlReaderDelegator.ReadElementContentAsDouble() + 0x19 bytes  
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.ReadValue(System.Type type, string name, string ns, System.Runtime.Serialization.XmlObjectSerializerReadContext context, System.Runtime.Serialization.XmlReaderDelegator xmlReader) + 0x138 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.ReadMemberAtMemberIndex(System.Runtime.Serialization.ClassDataContract classContract, ref object objectLocal, System.Runtime.Serialization.DeserializedObject desObj) + 0xc4 bytes    
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.ReadClass(System.Runtime.Serialization.DeserializedObject desObj, System.Runtime.Serialization.ClassDataContract classContract, int membersRead) + 0xf3 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.Deserialize(System.Runtime.Serialization.XmlObjectSerializerReadContext context) + 0x36 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.InitializeCallStack(System.Runtime.Serialization.DataContract clContract, System.Runtime.Serialization.XmlReaderDelegator xmlReaderDelegator, System.Runtime.Serialization.XmlObjectSerializerReadContext xmlObjContext, System.Xml.XmlDictionaryString[] memberNamesColl, System.Xml.XmlDictionaryString[] memberNamespacesColl) + 0x77 bytes    
System.Runtime.Serialization.dll!System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Runtime.Serialization.XmlObjectSerializerReadContext context) + 0x5d bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlReaderDelegator reader) + 0x3 bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(System.Runtime.Serialization.XmlReaderDelegator reader, string name, string ns, ref System.Runtime.Serialization.DataContract dataContract) + 0x10e bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Type declaredType, System.Runtime.Serialization.DataContract dataContract, string name, string ns) + 0xb bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalReadObject(System.Runtime.Serialization.XmlReaderDelegator xmlReader, bool verifyObjectName) + 0x124 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(System.Runtime.Serialization.XmlReaderDelegator reader, bool verifyObjectName) + 0xe bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.ReadObject(System.Xml.XmlDictionaryReader reader) + 0x7 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.ReadObject(System.IO.Stream stream) + 0x17 bytes  
System.Windows.dll!System.IO.IsolatedStorage.IsolatedStorageSettings.Reload() + 0xa3 bytes  
System.Windows.dll!System.IO.IsolatedStorage.IsolatedStorageSettings.IsolatedStorageSettings(bool useSiteSettings) + 0x20 bytes 
System.Windows.dll!System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.get() + 0xd bytes  

Answer 1:

它看起来像会有地理座标可能不是可序列化。 你可以尝试用两个双打的存储创建自己的类,然后将其转换回上检索。



Answer 2:

首先确保你的代码将运行无异常,但不保存到IsolatedStorage。 然后,你可以尝试只设置值,而不是做一个补充。

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Names"] = Names;
settings.Save();


文章来源: FormatException with IsolatedStorageSettings