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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 58 - (show annotations) (download)
Fri Mar 8 12:20:50 2013 UTC (7 years, 6 months ago) by william
File size: 2303 byte(s)

1 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 public class XMLTVSerializer<T> : IXMLTVSerializer<T>
14 {
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 xmltv_logger.Debug.WriteLine("Serializing data to file: '{0}'", file);
22 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 xmltv_logger.Debug.WriteLine("Serializing data to stream");
30 BinaryFormatter bin = new BinaryFormatter();
31 bin.Serialize(stream, data);
32 xmltv_logger.Debug.WriteLine("Serialized data to stream");
33 }
34 catch (Exception ex) { xmltv_logger.Error.WriteLine(ex.ToString()); return false; }
35 return true;
36 }
37 public T DeSerialize(string file, out bool status)
38 {
39 T unpacked_data = default(T);
40 xmltv_logger.Debug.WriteLine("DeSerializing data from file: '{0}'", file);
41 using (Stream stream = File.Open(file, FileMode.Open)) { unpacked_data = DeSerialize(stream, out status); }
42 return unpacked_data;
43 }
44 public T DeSerialize(Stream stream, out bool status)
45 {
46 T unpacked_data = default(T);
47 try
48 {
49 xmltv_logger.Debug.WriteLine("DeSerializing data from stream");
50 BinaryFormatter bin = new BinaryFormatter();
51 unpacked_data = (T)bin.Deserialize(stream);
52 xmltv_logger.Debug.WriteLine("DeSerialized data from stream");
53 }
54 catch (Exception ex) { xmltv_logger.Error.WriteLine(ex.ToString()); status = false; }
55 status = true;
56 return unpacked_data;
57 }
58 #endregion
59 }
60 }

  ViewVC Help
Powered by ViewVC 1.1.22