using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using libxmltv.Interfaces; using Enterprise.Logging; using System.Runtime.Serialization.Formatters.Binary; using System.Windows.Forms; namespace libxmltv.Core { public class XMLTVSerializer : IXMLTVSerializer { private T data; public XMLTVSerializer(T Data) { data = Data; } #region IXMLTVSerializer members public bool Serialize(string file) { bool retVal = false; using (Stream stream = File.Open(file, FileMode.Create)) { retVal = Serialize(stream); } return retVal; } public bool Serialize(Stream stream) { try { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, data); } catch (Exception ex) { xmltv_logger.Log.Error.WriteLine(ex.ToString()); return false; } return true; } public bool DeSerialize(string file, out T unpacked_data) { unpacked_data = default(T); bool retVal = false; using (Stream stream = File.Open(file, FileMode.Open)) { retVal = DeSerialize(stream, out unpacked_data); } return retVal; } public bool DeSerialize(Stream stream, out T unpacked_data) { unpacked_data = default(T); try { BinaryFormatter bin = new BinaryFormatter(); unpacked_data = (T)bin.Deserialize(stream); } catch (Exception ex) { xmltv_logger.Log.Error.WriteLine(ex.ToString()); return false; } return true; } #endregion } }