diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim.GenericConfig/Xml/XmlConfig.cs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/OpenSim.GenericConfig/Xml/XmlConfig.cs b/OpenSim.GenericConfig/Xml/XmlConfig.cs new file mode 100644 index 0000000..39321cc --- /dev/null +++ b/OpenSim.GenericConfig/Xml/XmlConfig.cs | |||
@@ -0,0 +1,92 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Xml; | ||
5 | using OpenSim.Framework.Interfaces; | ||
6 | |||
7 | namespace OpenSim.GenericConfig | ||
8 | { | ||
9 | public class XmlConfig : IGenericConfig | ||
10 | { | ||
11 | private XmlDocument doc; | ||
12 | private XmlNode rootNode; | ||
13 | private XmlNode configNode; | ||
14 | private string fileName; | ||
15 | |||
16 | public XmlConfig(string filename) | ||
17 | { | ||
18 | fileName = filename; | ||
19 | } | ||
20 | |||
21 | public void LoadData() | ||
22 | { | ||
23 | doc = new XmlDocument(); | ||
24 | try | ||
25 | { | ||
26 | XmlTextReader reader = new XmlTextReader(fileName); | ||
27 | reader.WhitespaceHandling = WhitespaceHandling.None; | ||
28 | doc.Load(reader); | ||
29 | reader.Close(); | ||
30 | } | ||
31 | catch (Exception e) | ||
32 | { | ||
33 | Console.WriteLine(e.Message); | ||
34 | return; | ||
35 | } | ||
36 | try | ||
37 | { | ||
38 | rootNode = doc.FirstChild; | ||
39 | if (rootNode.Name != "Root") | ||
40 | throw new Exception("Error: Invalid .xml File. Missing <Root>"); | ||
41 | |||
42 | configNode = rootNode.FirstChild; | ||
43 | if (configNode.Name != "Config") | ||
44 | throw new Exception("Error: Invalid .xml File. <Root> first child should be <Config>"); | ||
45 | |||
46 | } | ||
47 | catch (Exception e) | ||
48 | { | ||
49 | Console.WriteLine(e.Message); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | public string GetAttribute(string attributeName) | ||
54 | { | ||
55 | string result = ""; | ||
56 | if (configNode.Attributes[attributeName] != null) | ||
57 | { | ||
58 | result = ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value; | ||
59 | } | ||
60 | return result; | ||
61 | } | ||
62 | |||
63 | public bool SetAttribute(string attributeName, string attributeValue) | ||
64 | { | ||
65 | if (configNode.Attributes[attributeName] != null) | ||
66 | { | ||
67 | configNode.Attributes.GetNamedItem(attributeName).Value = attributeValue; | ||
68 | } | ||
69 | else | ||
70 | { | ||
71 | XmlAttribute attri; | ||
72 | attri = doc.CreateAttribute(attributeName); | ||
73 | attri.Value = attributeValue; | ||
74 | configNode.Attributes.Append(attri); | ||
75 | } | ||
76 | return true; | ||
77 | } | ||
78 | |||
79 | public void Commit() | ||
80 | { | ||
81 | doc.Save(fileName); | ||
82 | } | ||
83 | |||
84 | public void Close() | ||
85 | { | ||
86 | configNode = null; | ||
87 | rootNode = null; | ||
88 | doc = null; | ||
89 | } | ||
90 | |||
91 | } | ||
92 | } | ||