/[xmltv_parser]/trunk/libxmltv/Core/XMLTVSerializer.cs
ViewVC logotype

Annotation of /trunk/libxmltv/Core/XMLTVSerializer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 118 - (hide annotations) (download)
Sun Mar 10 17:41:52 2013 UTC (7 years, 7 months ago) by william
File size: 2305 byte(s)

1 william 49 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.IO;
6     using libxmltv.Interfaces;
7     using Enterprise.Logging;
8     using System.Runtime.Serialization.Formatters.Binary;
9     using System.Windows.Forms;
10    
11     namespace libxmltv.Core
12     {
13 william 118 internal class XMLTVSerializer<T> : IXMLTVSerializer<T>
14 william 49 {
15     private T data;
16     public XMLTVSerializer(T Data) { data = Data; }
17     #region IXMLTVSerializer<T> members
18     public bool Serialize(string file)
19     {
20     bool retVal = false;
21 william 58 xmltv_logger.Debug.WriteLine("Serializing data to file: '{0}'", file);
22 william 49 using (Stream stream = File.Open(file, FileMode.Create)) { retVal = Serialize(stream); }
23     return retVal;
24     }
25     public bool Serialize(Stream stream)
26     {
27     try
28     {
29 william 58 xmltv_logger.Debug.WriteLine("Serializing data to stream");
30 william 49 BinaryFormatter bin = new BinaryFormatter();
31     bin.Serialize(stream, data);
32 william 58 xmltv_logger.Debug.WriteLine("Serialized data to stream");
33 william 49 }
34 william 55 catch (Exception ex) { xmltv_logger.Error.WriteLine(ex.ToString()); return false; }
35 william 49 return true;
36     }
37 william 50 public T DeSerialize(string file, out bool status)
38 william 49 {
39 william 50 T unpacked_data = default(T);
40 william 58 xmltv_logger.Debug.WriteLine("DeSerializing data from file: '{0}'", file);
41 william 50 using (Stream stream = File.Open(file, FileMode.Open)) { unpacked_data = DeSerialize(stream, out status); }
42     return unpacked_data;
43 william 49 }
44 william 50 public T DeSerialize(Stream stream, out bool status)
45 william 49 {
46 william 50 T unpacked_data = default(T);
47 william 49 try
48     {
49 william 58 xmltv_logger.Debug.WriteLine("DeSerializing data from stream");
50 william 49 BinaryFormatter bin = new BinaryFormatter();
51     unpacked_data = (T)bin.Deserialize(stream);
52 william 58 xmltv_logger.Debug.WriteLine("DeSerialized data from stream");
53 william 49 }
54 william 55 catch (Exception ex) { xmltv_logger.Error.WriteLine(ex.ToString()); status = false; }
55 william 50 status = true;
56     return unpacked_data;
57 william 49 }
58     #endregion
59     }
60     }

  ViewVC Help
Powered by ViewVC 1.1.22