public static bool SaveToXml(String path, C_VariableListTypes types) { String backupPath = String.Format("{0}.bak", path); bool saveResult = false; try { XmlSerializer writer = new XmlSerializer(typeof(C_VariableListTypes), IncludedTypes); if (!FileHelper.IsFilePathLegal(path)) { return(false); } if (!FileHelper.CheckPath(path)) { Directory.CreateDirectory(Path.GetDirectoryName(path)); File.Create(path).Close(); } File.Copy(path, backupPath, true); using (StreamWriter file = new StreamWriter(path)) { writer.Serialize(file, types); } saveResult = true; } catch (Exception ex) { try { File.Copy(path, String.Format("{0}.err", path), true); File.Copy(backupPath, path, true); } catch (Exception ex1) { LogWriter.Instance.WriteToLog(ex1, "Error restoring backup: {0}", backupPath); } LogWriter.Instance.WriteToLog(ex, "Error saving {0}", path); } return(saveResult); }
public bool LoadFromXml(String path, C_VariableListTypes types, C_VariableListContexts context) { bool loadResult = false; try { if (!FileHelper.IsFilePathLegal(path)) { return(false); } if (FileHelper.CheckPath(path)) { var eventHandlers = types.CollectionChanged; XmlSerializer reader = new XmlSerializer(typeof(C_VariableListTypes), IncludedTypes); using (StreamReader file = new StreamReader(path)) { types = (C_VariableListTypes)reader.Deserialize(file); } types.CollectionChanged = eventHandlers; loadResult = true; UpdateCollectionChanged(CollectionChangeAction.Refresh, "All"); foreach (C_Variable cvItem in types.Collection) { cvItem.SetParent(); } foreach (C_Variable cvItem in context.ContextCollection) { cvItem.SetParent(); } } } catch (Exception ex) { LogWriter.Instance.WriteToLog(ex, "Error loading C_VariableList.xml"); } return(loadResult); }