This commit is contained in:
171
PROMS/VEPROMS.CSLA.Library/Config/XMLProperties.cs
Normal file
171
PROMS/VEPROMS.CSLA.Library/Config/XMLProperties.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Xml;
|
||||
|
||||
namespace VEPROMS.CSLA.Library
|
||||
{
|
||||
public delegate string XMLPropertiesEvent(object sender, XMLPropertiesArgs args);
|
||||
[Serializable()]
|
||||
public class XMLProperties
|
||||
{
|
||||
#region Events
|
||||
public event XMLPropertiesEvent LookInAncestor;
|
||||
private string OnLookInAncestor(object sender, XMLPropertiesArgs args)
|
||||
{
|
||||
if (LookInAncestor != null) return LookInAncestor(sender, args);
|
||||
return string.Empty;
|
||||
}
|
||||
#endregion
|
||||
#region Constructors
|
||||
public XMLProperties()
|
||||
{
|
||||
_XmlContents = new XmlDocument();
|
||||
_XmlContents.LoadXml("<config/>");
|
||||
}
|
||||
public XMLProperties(string xml)
|
||||
{
|
||||
_XmlContents = new XmlDocument();
|
||||
_XmlContents.LoadXml(xml);
|
||||
}
|
||||
#endregion
|
||||
#region BusinessMethods
|
||||
[NonSerialized]
|
||||
private bool _ParentLookup=false;
|
||||
public bool ParentLookup
|
||||
{
|
||||
get { return _ParentLookup; }
|
||||
set { _ParentLookup = value; }
|
||||
}
|
||||
[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
|
||||
{
|
||||
if (_ParentLookup)
|
||||
return OnLookInAncestor(this, new XMLPropertiesArgs(group, item));
|
||||
XmlNode xn = GetGroup(group);
|
||||
if (xn == null) return OnLookInAncestor(this,new XMLPropertiesArgs(group,item));
|
||||
XmlNode xa = GetItem(xn, item);
|
||||
if (xa == null) return OnLookInAncestor(this, new XMLPropertiesArgs(group, item));
|
||||
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()
|
||||
{
|
||||
if (XmlContents == null) return null;
|
||||
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
|
||||
}
|
||||
public partial class XMLPropertiesArgs
|
||||
{
|
||||
#region Business Methods
|
||||
private string _Group;
|
||||
public string Group
|
||||
{
|
||||
get { return _Group; }
|
||||
set { _Group = value; }
|
||||
}
|
||||
private string _Item;
|
||||
public string Item
|
||||
{
|
||||
get { return _Item; }
|
||||
set { _Item = value; }
|
||||
}
|
||||
#endregion
|
||||
#region Factory Methods
|
||||
private XMLPropertiesArgs() { ;}
|
||||
public XMLPropertiesArgs(string group, string item)
|
||||
{
|
||||
_Group=group;
|
||||
_Item=item;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user