2006-11-14 14:33:33 +00:00

128 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
namespace Volian.CSLA.Library
{
[Serializable()]
class XMLProperties
{
#region Constructors
public XMLProperties()
{
_XmlContents = new XmlDocument();
_XmlContents.LoadXml("<config/>");
}
public XMLProperties(string xml)
{
_XmlContents = new XmlDocument();
_XmlContents.LoadXml(xml);
}
#endregion
#region BusinessMethods
[NonSerialized]
XmlDocument _XmlContents;
public XmlDocument XmlContents
{
get { return _XmlContents; }
}
private XmlNode GetGroup(string group)
{
XmlNodeList xl = _XmlContents.DocumentElement.SelectNodes(string.Format("//{0}", group));
switch (xl.Count)
{
case 0: // No nodes found
return null;
case 1: // Found the node
XmlNode xn = xl[0];
if (xn.GetType() == typeof(XmlElement)) return xn;
throw new XmlPropertiesException("Retrieved {0} while looking for XmlElement //{1}", xn.GetType().ToString(), group);
default: // Found more than 1 node
throw new XmlPropertiesException("Found more than one node //{0}", group);
}
}
private XmlAttribute GetItem(XmlNode xx, string item)
{
XmlNodeList xl = xx.SelectNodes(string.Format("@{0}", item));
switch (xl.Count)
{
case 0: // No nodes found
return null;
case 1: // Found the node
XmlNode xn = xl[0];
if (xn.GetType() == typeof(XmlAttribute)) return (XmlAttribute)xn;
throw new XmlPropertiesException("Retrieved {0} while looking for XmlAttribute @{1}", xn.GetType().ToString(), item);
default: // Found more than 1 node
throw new XmlPropertiesException("Found more than one node @{0}", item);
}
}
public string this[string group, string item]
{
get
{
XmlNode xn = GetGroup(group);
if (xn == null) return string.Empty;
XmlNode xa = GetItem(xn, item);
if (xa == null) return string.Empty;
return xa.InnerText;
}
set
{
if (value == null) value = string.Empty;
XmlNode xn = GetGroup(group);
if (xn == null)
{
if (value != string.Empty)// Add Group and Item
{
xn = _XmlContents.DocumentElement.AppendChild(_XmlContents.CreateElement(group));
XmlAttribute xa = xn.Attributes.Append(_XmlContents.CreateAttribute(item));
xa.Value = value;
}
}
else
{
XmlAttribute xa = GetItem(xn, item);
if (xa == null)
{
if (value != string.Empty) // Add Item
{
xa = xn.Attributes.Append(_XmlContents.CreateAttribute(item));
xa.Value = value;
}
}
else
{
if (value != string.Empty) // Add Item
{
xa.Value = value;
}
else
{
xn.Attributes.Remove(xa);
if (xn.Attributes.Count == 0)
_XmlContents.DocumentElement.RemoveChild(xn);
}
}
}
}
}
public override string ToString()
{
return _XmlContents.OuterXml;
}
#endregion
#region XmlPropertiesException
public class XmlPropertiesException : Exception
{
public XmlPropertiesException() : base() { ;}
public XmlPropertiesException(string message) : base(message) { ;}
public XmlPropertiesException(string messageFormat,params object [] args) : base(string.Format(messageFormat,args)) { ;}
public XmlPropertiesException(SerializationInfo info, StreamingContext context) : base(info, context) { ;}
public XmlPropertiesException(string message,Exception innerException) : base(message, innerException) { ;}
}
#endregion
}
}