-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlSerialiationDeserilation.cs
52 lines (47 loc) · 1.08 KB
/
XmlSerialiationDeserilation.cs
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
using System;
using System.IO;
using System.Xml.Serialization;
namespace LiveSplit.TwitchPredictions
{
public class XmlSerialiationDeserilation
{
public static T ReadFromXMLFile<T>(string filePath) where T : new()
{
StreamReader reader = null;
try
{
if (!File.Exists(filePath))
return new T();
XmlSerializer serializer = new XmlSerializer(typeof(T));
reader = new StreamReader(filePath);
return (T)serializer.Deserialize(reader);
}
finally
{
if (reader != null)
reader.Close();
}
}
public static void SaveObjectToXML<T>(T objectToStore, string filePath) where T : new()
{
if (!Directory.Exists(Directory.GetParent(filePath).FullName))
Directory.CreateDirectory(Directory.GetParent(filePath).FullName);
StreamWriter writter = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
writter = new StreamWriter(filePath);
serializer.Serialize(writter, objectToStore);
}
catch (Exception e)
{
throw e;
}
finally
{
if (writter != null)
writter.Close();
}
}
}
}