// ========================================================================
// Copyright 2006 - Volian Enterprises, Inc. All rights reserved.          
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
// ------------------------------------------------------------------------
// $Workfile: $     $Revision: $                                           
// $Author: $   $Date: $                                                   
//                                                                         
// $History: $                                                             
// ========================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace DataLoader
{
	class ConfigInfo
	{
		private XmlDocument xmldoc;
		public ConfigInfo(string xml)
		{
            xmldoc = new XmlDocument();
            if (xml == null)
                xmldoc.LoadXml("");
            else
                xmldoc.LoadXml(xml);
		}
		public ConfigInfo(string xml, string ename, string aname, string avalue)
		{
            xmldoc = new XmlDocument();
            if (xml == null)
                xmldoc.LoadXml("");
            else
                xmldoc.LoadXml(xml);
			AddItem(ename, aname.Replace(' ','_'), avalue);
		}
		public bool AddItem(string ename, string aname, string avalue)
		{
			if (aname != null && aname != "")
			{
                //if (xmldoc == null)
                //{
                //    xmldoc = new XmlDocument();
                //    xmldoc.AppendChild(xmldoc.CreateElement("ConfigInfo"));
                //}
                // see if ename element exists, use it to add attributes, 
                // otherwise, create the element.
                XmlNode nxml = null;
                XmlNodeList xl = xmldoc.DocumentElement.SelectNodes(string.Format("//{0}", ename));
                switch (xl.Count)
                {
                    case 0: // No nodes found
                        nxml = xmldoc.DocumentElement.AppendChild(xmldoc.CreateElement(ename));
                        break;
                    default: // Found the node
                        nxml = xl[0];
                        if (nxml.GetType() != typeof(XmlElement))
                        {
							frmLoader._MyLog.ErrorFormat("Invalid xml element type when migrating config data - element = {0}, name = {1} , value = {2}", ename, aname, avalue);
                            return false;
                        }
                        break;
                }
                XmlAttribute xa = nxml.Attributes.Append(xmldoc.CreateAttribute(aname.Replace(' ', '_')));
                xa.Value = avalue;
				return true;
			}
			return false;
		}
		public int ItemCount
		{
			get { return xmldoc.DocumentElement.ChildNodes.Count; }
		}
		public override string ToString()
		{
			if (xmldoc != null) return xmldoc.InnerXml;
			else return null;
		}
	}
}