Repository navigation
Expand file tree
/
Copy pathXMLSerializationServices.cs
More file actions
87 lines (81 loc) · 2.87 KB
/
Copy pathXMLSerializationServices.cs
File metadata and controls
87 lines (81 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace SerializationServices
{
public static class XMLSerializationServices
{
/// <summary>
/// Serializes the data as XML in the object to the designated file path
/// </summary>
/// <typeparam name="T">Type of Object to serialize</typeparam>
/// <param name="dataToSerialize">Object to serialize</param>
/// <param name="filePath">FilePath for the XML file</param>
public static void SerializeToFile<T>(T dataToSerialize, string filePath)
{
using (Stream stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite))
{
var writer = new XmlTextWriter(stream, Encoding.Default) { Formatting = Formatting.Indented };
new XmlSerializer(typeof(T)).Serialize(writer, dataToSerialize);
writer.Close();
}
}
public static void SerializeToStream<T>(T dataToSerialize, Stream st)
{
var writer = new XmlTextWriter(st, Encoding.Default) { Formatting = Formatting.Indented };
new XmlSerializer(typeof(T)).Serialize(writer, dataToSerialize);
writer.Close();
}
public static string Serialize<T>(T dataToSerialize)
{
using (var tw = new StringWriter())
{
new XmlSerializer(typeof(T)).Serialize(tw, dataToSerialize);
return tw.ToString();
}
}
/// <summary>
/// Deserializes the data in the XML file into an object
/// </summary>
/// <typeparam name="T">Type of object to deserialize</typeparam>
/// <param name="filePath">FilePath to XML file</param>
/// <returns>Object containing deserialized data</returns>
public static T DeserializeFromFile<T>(string filePath)
{
try
{
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
return (T)(new XmlSerializer(typeof(T))).Deserialize(stream);
}
catch
{
return Activator.CreateInstance<T>();
}
}
public static T DeserializeFromStream<T>(Stream st)
{
try
{
return (T)(new XmlSerializer(typeof(T))).Deserialize(st);
}
catch
{
return Activator.CreateInstance<T>();
}
}
public static T Deserialize<T>(string str)
{
try
{
using (var reader = new StringReader(str))
return (T)(new XmlSerializer(typeof(T))).Deserialize(reader);
}
catch
{
return Activator.CreateInstance<T>();
}
}
}
}