-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCityVitalsWatchSerializer.cs
63 lines (55 loc) · 2.02 KB
/
CityVitalsWatchSerializer.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
53
54
55
56
57
58
59
60
61
62
63
namespace CityVitalsWatch {
using System.IO;
using System.Xml.Serialization;
/// <summary>
/// Provides methods to load and save <see cref="CityVitalsWatchSettings"/> instances using XML serialization.
/// </summary>
public static class CityVitalsWatchSerializer {
/// <summary>
/// The name of the XML file.
/// </summary>
private static readonly string SettingsFileName = "CityVitalsWatchSettings.xml";
/// <summary>
/// Loads the settings from the XML file.
/// </summary>
/// <returns>The loaded settings.</returns>
public static CityVitalsWatchSettings LoadSettings() {
CityVitalsWatchSettings settings = null;
FileStream stream = null;
try {
XmlSerializer serializer = new XmlSerializer(typeof(CityVitalsWatchSettings));
stream = new FileStream(SettingsFileName, FileMode.Open);
settings = (CityVitalsWatchSettings)serializer.Deserialize(stream);
}
catch {
settings = new CityVitalsWatchSettings();
}
finally {
if (stream != null) {
stream.Close();
}
}
return settings;
}
/// <summary>
/// Saves the provided settings to the XML file.
/// </summary>
/// <param name="settings">The settings to save.</param>
public static void SaveSettings(CityVitalsWatchSettings settings) {
StreamWriter stream = null;
try {
XmlSerializer serializer = new XmlSerializer(typeof(CityVitalsWatchSettings));
stream = new StreamWriter(SettingsFileName);
serializer.Serialize(stream, settings);
}
catch {
// Do nothing on exception
}
finally {
if (stream != null) {
stream.Close();
}
}
}
}
}