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 |
using (Stream stream = File.Open(file, FileMode.Create)) { retVal = Serialize(stream); } |
22 |
return retVal; |
23 |
} |
24 |
public bool Serialize(Stream stream) |
25 |
{ |
26 |
try |
27 |
{ |
28 |
BinaryFormatter bin = new BinaryFormatter(); |
29 |
bin.Serialize(stream, data); |
30 |
} |
31 |
catch (Exception ex) { xmltv_logger.Log.Error.WriteLine(ex.ToString()); return false; } |
32 |
return true; |
33 |
} |
34 |
public T DeSerialize(string file, out bool status) |
35 |
{ |
36 |
T unpacked_data = default(T); |
37 |
using (Stream stream = File.Open(file, FileMode.Open)) { unpacked_data = DeSerialize(stream, out status); } |
38 |
return unpacked_data; |
39 |
} |
40 |
public T DeSerialize(Stream stream, out bool status) |
41 |
{ |
42 |
T unpacked_data = default(T); |
43 |
try |
44 |
{ |
45 |
BinaryFormatter bin = new BinaryFormatter(); |
46 |
unpacked_data = (T)bin.Deserialize(stream); |
47 |
} |
48 |
catch (Exception ex) { xmltv_logger.Log.Error.WriteLine(ex.ToString()); status = false; } |
49 |
status = true; |
50 |
return unpacked_data; |
51 |
} |
52 |
#endregion |
53 |
} |
54 |
} |