/// <summary> /// Serializes an AlarmRecord object to XML document /// </summary> /// <param name="properties">AlarmRecordDictionary object to serialize</param> /// <seealso cref="IDictionary<string, AlarmRecord>"> /// <returns> Task return type </returns> public Task SerializeAlarmRecordAsync(IDictionary <string, AlarmRecord> properties) { //properties = new Dictionary<string, object>(properties); // Serialize property dictionary to local storage // Make sure to use Internal return(Task.Run(() => { Stream alarmFileStream = null; var fileOpener = new FileOpener(); try { alarmFileStream = fileOpener.OpenFile(AlarmRecordStoreFile, System.IO.FileMode.Create); using (XmlDictionaryWriter alarmWriter = XmlDictionaryWriter.CreateBinaryWriter(alarmFileStream)) { var dataContractSerializer = new DataContractSerializer(typeof(Dictionary <string, AlarmRecord>)); dataContractSerializer.WriteObject(alarmWriter, properties); alarmFileStream = null; alarmWriter.Flush(); } } catch (Exception e) { Debug.WriteLine("[Alarm serialization error] " + e.Message); } finally { if (alarmFileStream != null) { alarmFileStream.Dispose(); } } return; })); }
/// <summary> /// Deserializes an AlarmRecord object from XML document /// </summary> /// <returns> a collection of <string, AlarmRecord></string> </returns> public IDictionary <string, AlarmRecord> DeserializeAlarmRecord() { Stream alarmFileStream = null; var fileOpener = new FileOpener(); alarmFileStream = fileOpener.OpenFile(AlarmRecordStoreFile, System.IO.FileMode.OpenOrCreate); if (alarmFileStream.Length == 0) { return(null); } using (XmlDictionaryReader alarmReader = XmlDictionaryReader.CreateBinaryReader(alarmFileStream, XmlDictionaryReaderQuotas.Max)) { var dataContractSerializer = new DataContractSerializer(typeof(Dictionary <string, AlarmRecord>)); Object ret1 = dataContractSerializer.ReadObject(alarmReader); alarmFileStream = null; return((IDictionary <string, AlarmRecord>)ret1); } }