Beispiel #1
0
        public virtual void Save()
        {
            try {
                if (!File.Exists(Filename))
                {
                    CreateFileForFirstTime();
                }
#if !__WP8__
                if (_useSimplyXmlSerialization)
                {
                    XmlProvider.WriteToFile(Filename, new SerializableDictionarySurrogate <T1, T2>(this));
                }
                else
                {
                    XmlProvider.DeepWriteToFile(Filename, new SerializableDictionarySurrogate <T1, T2>(this));
                }
#else
                XmlProvider.WriteToFile(Filename, new SerializableDictionarySurrogate <T1, T2>(this));
#endif
            } catch (Exception error) {
                throw new SoftwareException(
                          error,
                          "Failed to write FileDictionary '{0}'.",
                          Filename
                          );
            }
        }
Beispiel #2
0
        public virtual void Load()
        {
            #region Pre-conditions
            Debug.Assert(Filename != null);
            #endregion
            try {
                if (File.Exists(Filename))
                {
#if !__WP8__
                    var surrogate = _useSimplyXmlSerialization ?
                                    XmlProvider.ReadFromFile <SerializableDictionarySurrogate <T1, T2> >(Filename) :
                                    XmlProvider.DeepReadFromFile <SerializableDictionarySurrogate <T1, T2> >(Filename);
#else
                    var surrogate = XmlProvider.ReadFromFile <SerializableDictionarySurrogate <T1, T2> >(Filename);
#endif
                    surrogate.ToDictionary(this);
                }
            } catch (Exception error) {
                #region Attempt to recreate file
                try {
                    File.Delete(Filename);
                    CreateFileForFirstTime();
                    Save();
                    if (File.Exists(Filename))
                    {
#if !__WP8__
                        var xxx = _useSimplyXmlSerialization ?
                                  XmlProvider.ReadFromFile <SerializableDictionarySurrogate <T1, T2> >(Filename) :
                                  XmlProvider.DeepReadFromFile <SerializableDictionarySurrogate <T1, T2> >(Filename);
                        if (xxx != null)
                        {
                            xxx.ToDictionary(this);
                        }
#else
                        XmlProvider.ReadFromFile <SerializableDictionarySurrogate <T1, T2> >(Filename).ToDictionary(this);
#endif
                    }
                    #endregion
                } catch (Exception innerError) {
                    throw new SoftwareException(
                              innerError,
                              "Failed to load and recreate FileDictionary '{0}'.",
                              Filename
                              );
                }
            }
        }
 public static T FromXml <T>(this string source)
 {
     return(XmlProvider.ReadFromString <T>(source));
 }
 public static string ToXml <T>(this T source)
 {
     return(XmlProvider.WriteToString(source));
 }