我写的一个xml配置类


一直不喜欢.net自带的配置类,所以本着自已动手,丰衣足食的思想自已写了一个配置类,也可以满足大多数的需求了。

  1using System;
  2using System.IO;
  3using System.Xml;
  4
  5namespace ******.Utils
  6{
  7    public class XmlConfiguration
  8    {
  9        private string _filename;
 10        private XmlDocument xmldocument = null;
 11
 12        private XmlNode getGroup(String groupName, bool createGroup)
 13        {
 14            String[] groups = groupName.Split(new Char[] '\\''/' });
 15            XmlNode groupa = this.xmldocument.DocumentElement;
 16            XmlNode group = null;
 17            for (int i = 0; i < groups.Length; i++)
 18            {
 19                if (groups[i] == null || groups[i].Equals("")) continue;
 20                group = groupa.SelectSingleNode(groups[i]);
 21                if (group == null)
 22                {
 23                    if (createGroup)
 24                        group = groupa.AppendChild(this.xmldocument.CreateElement(groups[i]));
 25                    else
 26                        break;
 27                }

 28                groupa = group;
 29            }

 30            return group;
 31        }

 32
 33        public XmlConfiguration(): this(null)
 34        {
 35
 36        }

 37
 38        public XmlConfiguration(string fileName)
 39        {
 40            LoadFromFile(fileName);
 41        }

 42
 43        public void LoadFromFile(string fileName)
 44        {
 45            if (fileName == null || fileName == "")
 46            {
 47                fileName = "app.config.xml";
 48            }

 49            this._filename = fileName;
 50            this.xmldocument = new XmlDocument();
 51            if (File.Exists(fileName))
 52                this.xmldocument.Load(fileName);
 53            if (this.xmldocument.DocumentElement == null)
 54            {
 55                this.xmldocument.AppendChild(this.xmldocument.CreateXmlDeclaration("1.0""UTF-8"""));
 56                this.xmldocument.AppendChild(this.xmldocument.CreateElement("configuration"));
 57            }
            
 58        }

 59
 60        public void Save()
 61        {
 62            this.xmldocument.Save(this._filename);
 63
 64        }

 65
 66        public string[] GetGroups()
 67        {
 68            if (!this.xmldocument.DocumentElement.HasChildNodes)
 69                return null;
 70            string str = "";
 71            for (int i = 0; i < this.xmldocument.DocumentElement.ChildNodes.Count; i++)
 72            {
 73                if (this.xmldocument.DocumentElement.ChildNodes[i] is XmlElement)
 74                {
 75                    if (str == "")
 76                        str = this.xmldocument.DocumentElement.ChildNodes[i].Name;
 77                    else
 78                        str = str + "," + this.xmldocument.DocumentElement.ChildNodes[i].Name;
 79                }

 80            }

 81            return str.Split(',');
 82        }

 83
 84        public string[] GetGroups(string groupname)
 85        {
 86            if (groupname == null || groupname == "" || this.xmldocument.DocumentElement == null)
 87                return null;
 88
 89            XmlNode group = this.getGroup(groupname,false);
 90            if (group == nullreturn null;                
 91
 92            string str = "";
 93            for (int i = 0; i < group.ChildNodes.Count; i++)
 94            {
 95                if (group.ChildNodes[i] is XmlElement)
 96                {
 97                    if (str == "")
 98                        str = group.ChildNodes[i].Name;
 99                    else
100                        str = str + "," + group.ChildNodes[i].Name;
101                }

102            }

103            return str.Split(',');
104        }

105
106        public bool WriteValue(string groupname, string value)
107        {
108            if (groupname == null || groupname == "" || this.xmldocument.DocumentElement == null)
109                return false;
110
111            XmlNode group = this.getGroup(groupname, true);
112            if (group == nullreturn false;
113
114            group.InnerText = value;
115            return true;
116        }

117
118        public string ReadValue(string groupname)
119        {
120            if (groupname == null || groupname == "")
121                return null;
122
123            XmlNode group = this.getGroup(groupname, false);
124            if (group == nullreturn null;
125            return group.InnerText;
126        }

127
128        public void SetProperty(string groupname, string propertyname, string value)
129        {
130            if (groupname == null || groupname == "" || this.xmldocument.DocumentElement == null)
131                return;
132
133            XmlNode group = this.getGroup(groupname, true);
134            if (group == nullreturn;    
135
136            XmlAttribute attribute = group.Attributes[propertyname];
137            if (attribute == null)
138            {
139                attribute = this.xmldocument.CreateAttribute(propertyname);
140                group.Attributes.Append(attribute);
141            }

142            attribute.Value = value;
143        }

144
145        public string GetProperty(string groupname, string propertyname)
146        {
147            if (groupname == null || groupname == "" || this.xmldocument.DocumentElement == null)
148                return null;
149
150            XmlNode group = this.getGroup(groupname, false);
151            if (group == nullreturn null;   
152
153            XmlAttribute attribute = group.Attributes[propertyname];
154            if (attribute == null)
155            {
156                return null;
157            }

158            return attribute.Value;
159        }

160
161        public void RemoveAll()
162        {
163            this.xmldocument.DocumentElement.RemoveAll();
164        }

165
166        public string FileName
167        {
168            get
169            {
170                if (this._filename == null || this._filename == "")
171                {
172                    string configPath = "app.config.xml";
173                }

174                else
175                {
176                    return this._filename;
177                }

178            }

179        }

180    }

181}

使用方法:
XmlConfiguration config = new XmlConfiguration("config.xml");
//
config.WriteValue("/config/url","http://www.sina.com.cn");
//
string value = config.ReadValue("/config/url");

posted on 2007-07-13 15:51  王者归来  阅读(416)  评论(0)    收藏  举报

导航