예제 #1
0
        /// <summary>
        /// Deserializes an AlarmRecord object from XML document
        /// </summary>
        /// <returns> a collection of <string, AlarmRecord></string> </returns>
        public static IDictionary <string, AlarmRecord> DeserializeAlarmRecord()
        {
            Console.WriteLine("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);
            }
        }
예제 #2
0
        /// <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 static Task SerializeAlarmRecordAsync(IDictionary <string, AlarmRecord> properties)
        {
            Console.WriteLine("SerializeAlarmRecordAsync");
            //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)
                {
                    Console.WriteLine("[Alarm serialization error] " + e.Message);
                }
                finally
                {
                    if (alarmFileStream != null)
                    {
                        alarmFileStream.Dispose();
                    }
                }

                return;
            }));
        }
예제 #3
0
        /// <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 static void SerializeAlarmRecord(IDictionary <string, AlarmRecord> properties)
        {
            Console.WriteLine("SerializeAlarmRecord");

            // Serialize property dictionary to local storage
            var fileOpener = new FileOpener();

            try
            {
                using (var alarmFileStream = fileOpener.OpenFile(AlarmRecordStoreFile, FileMode.Create))
                {
                    using (var alarmWriter = XmlDictionaryWriter.CreateBinaryWriter(alarmFileStream))
                    {
                        var dataContractSerializer = new DataContractSerializer(typeof(Dictionary <string, AlarmRecord>));
                        dataContractSerializer.WriteObject(alarmWriter, properties);
                        alarmWriter.Flush();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("[Alarm serialization error] " + e.Message);
            }
        }