-
Notifications
You must be signed in to change notification settings - Fork 13
/
ServerConfiguration.cs
84 lines (62 loc) · 2.4 KB
/
ServerConfiguration.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
namespace MemCacheDManager
{
public class ServerConfiguration
{
public void Load(string filename)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(filename);
XmlNode versionNode = xmlDocument.SelectSingleNode("/root/version");
Version version = new Version(versionNode.InnerText);
// Add logic to handle version mismatch and data file upgrading here.
XmlNode serversNode = xmlDocument.SelectSingleNode("/root/servers");
if (serversNode != null)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Business.ServerCollection));
StringReader stringReader = new StringReader(serversNode.InnerXml);
XmlReader xmlReader = XmlReader.Create(stringReader);
this._servers = (Business.ServerCollection)xmlSerializer.Deserialize(xmlReader);
}
}
public void Save(string filename)
{
XmlDocument xmlDocument = new XmlDocument();
XmlNode rootNode = xmlDocument.CreateElement("root");
xmlDocument.AppendChild(rootNode);
XmlNode versionNode = xmlDocument.CreateElement("version");
versionNode.InnerText = this.Version.ToString();
rootNode.AppendChild(versionNode);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Business.ServerCollection));
StringWriter stringWriter = new StringWriter();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.OmitXmlDeclaration = true;
XmlWriter xmlWriter = XmlTextWriter.Create(stringWriter, xmlWriterSettings);
xmlSerializer.Serialize(xmlWriter, this._servers);
XmlNode serversNode = xmlDocument.CreateElement("servers");
serversNode.InnerXml = stringWriter.GetStringBuilder().ToString();
rootNode.AppendChild(serversNode);
XmlTextWriter xmlDocumentWriter = new XmlTextWriter(filename, Encoding.Default);
xmlDocumentWriter.Formatting = Formatting.Indented;
xmlDocument.Save(xmlDocumentWriter);
xmlDocumentWriter.Close();
}
private Business.ServerCollection _servers = new MemCacheDManager.Business.ServerCollection();
public Business.ServerCollection Servers
{
get { return _servers; }
}
private string _version = Assembly.GetEntryAssembly().GetName().Version.ToString();
public string Version
{
get { return _version; }
}
}
}