XmlIni (C# MonoDevelop)

//  
//  XmlIni.cs
//  
//  Author: Ervin, Jung
//         <${AuthorEmail}>
// 
//  Copyright (c) 2011 Ervin, Jung
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//      example:
//
//		XmlIni Ini = new XmlIni("ini.xml", "myapp");
// 		
// 		int i = Ini.ReadInteger("section", "integer", 10);
// 		Console.WriteLine(i);
// 		Ini.WriteInteger("section", "integer", 20);
//
//		string a = Ini.ReadAttribute("section", "integer", "name", "defaultattr");
//		Console.WriteLine(a);
//		Ini.WriteAttribute("section", "integer", "name", "newattr");
//
//		double d = Ini.ReadDouble("section", "double", 1.2);
//		Console.WriteLine(d);
//		Ini.WriteDouble("section", "double", 20.2);
//		
//		bool b = Ini.ReadBool("section", "bool", false);
//		Console.WriteLine(b);
//		Ini.WriteBool("section", "bool", true);
// 
//		string s = Ini.ReadString("section", "string", "defstr");
//		Console.WriteLine(s);
//		Ini.WriteString("section", "string", "newstr");
//
 
using System;
using System.IO;
using System.Xml;
 
namespace Helpers
{
	public class XmlIni : XmlDocument
	{
		private string _FileName;
		private string _RootName;
		private XmlNode _Root = null;
		public XmlIni (string file_name, string root_name) : base()
		{
			_FileName = file_name;
			_RootName = root_name;
 
			LoadFromFile ();
			if (_Root == null) {
				XmlDeclaration xmlDeclaration = CreateXmlDeclaration ("1.0", "utf-8", null);
				InsertBefore (xmlDeclaration, DocumentElement);
				_Root = CreateElement (root_name);
				AppendChild (_Root);
			}
		}
 
		private void LoadFromFile ()
		{
			try {
				if (File.Exists (_FileName)) {
					Load (_FileName);
					_Root = SelectSingleNode (_RootName);
				}
			} catch (Exception e) {
				Console.WriteLine (e.Message);
			}
		}
 
		~XmlIni ()
		{
			try {
				Save (_FileName);
			} catch (Exception e) {
				Console.WriteLine (e.Message);
			}
		}
 
		public void EraseSection (string section)
		{
			XmlNode Section = _Root.SelectSingleNode (section);
			if (Section != null)
				Section.RemoveAll ();
		}
 
		public void WriteAttribute (string section, string item, string attr, string value)
		{
			XmlNode Section = _Root.SelectSingleNode (section);
			if (Section == null)
				return;
			XmlNode Item = Section.SelectSingleNode (item);
			if (Item == null)
				return;
 
			XmlAttribute Attr = CreateAttribute (attr);
			Attr.Value = value;
			Item.Attributes.Append (Attr);
		}
 
		public string ReadAttribute (string section, string item, string attr, string defval)
		{
			XmlNode Section = _Root.SelectSingleNode (section);
			if (Section == null)
				return defval;
 
			XmlNode Item = Section.SelectSingleNode (item);
			if (Item == null)
				return defval;
 
			if (Item.Attributes[attr] == null)
				return defval;
			return Item.Attributes[attr].Value.ToString ();
		}
 
		public void WriteString (string section, string item, string value)
		{
			XmlNode Section = _Root.SelectSingleNode (section);
			if (Section == null) {
				Section = CreateElement (section);
				_Root.AppendChild (Section);
			}
 
			XmlNode Item = Section.SelectSingleNode (item);
			if (Item == null) {
				Item = CreateElement (item);
				Section.AppendChild (Item);
			}
			Item.InnerText = value.Trim ();
		}
 
		public string ReadString (string section, string item, string defval)
		{
			XmlNode Section = _Root.SelectSingleNode (section);
			if (Section == null)
				return defval;
 
			XmlNode Item = Section.SelectSingleNode (item);
			if (Item == null)
				return defval;
 
			return Item.InnerText.Trim ();
		}
 
		public void WriteInteger (string section, string item, int value)
		{
			WriteString (section, item, value.ToString ());
		}
 
		public int ReadInteger (string section, string item, int defval)
		{
			try {
				return int.Parse (ReadString (section, item, defval.ToString ()));
			} catch (Exception e) {
				Console.WriteLine (e.Message);
				return defval;
			}
		}
 
		public void WriteDouble (string section, string item, double value)
		{
			WriteString (section, item, value.ToString ());
		}
 
		public double ReadDouble (string section, string item, double defval)
		{
			try {
				return double.Parse (ReadString (section, item, defval.ToString ()));
			} catch (Exception e) {
				Console.WriteLine (e.Message);
				return defval;
			}
		}
 
		public void WriteBool (string section, string item, bool value)
		{
			if (value)
				WriteString (section, item, "1");
			else
				WriteString (section, item, "0");
		}
 
		public bool ReadBool (string section, string item, bool defval)
		{
			string def = "0";
			if (defval)
				def = "1";
 
			return ReadString (section, item, def) == "1";
		}
	}
}