// ======================================================================== // 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(' ', '_'))); //added by jcb 20131216 //aname = SanitizeXmlString(aname); //XmlAttribute xa = nxml.Attributes.Append(xmldoc.CreateAttribute(aname)); //end added by jcb 20131216 xa.Value = avalue; return true; } return false; } //added by jcb 20131216 public string SanitizeXmlString(string xml) { if (xml == null) throw new ArgumentNullException("xml"); xml = xml.Replace(' ', '_').Replace("/", "_fslash_").Replace("(", "_lparen_").Replace((char)0xA0, '_').Replace((char)0x20, '_').Replace(":", "_colon_"); StringBuilder buffer = new StringBuilder(xml.Length); foreach (char c in xml) { if (IsLegalXmlChar(c)) buffer.Append(c); } return buffer.ToString(); } /// /// Whether a given character is allowed by XML 1.0. /// public bool IsLegalXmlChar(int character) { return ( character == 0x9 /* == '\t' == 9 */ || character == 0xA /* == '\n' == 10 */ || character == 0xD /* == '\r' == 13 */ || (character >= 0x20 && character <= 0xD7FF) || (character >= 0xE000 && character <= 0xFFFD) || (character >= 0x10000 && character <= 0x10FFFF) ); } //end added by jcb 20131216 public int ItemCount { get { return xmldoc.DocumentElement.ChildNodes.Count; } } public override string ToString() { if (xmldoc != null) return xmldoc.InnerXml; else return null; } public void AddSlaveNode(int index) { XmlElement slaves = xmldoc.SelectSingleNode("Config/Slaves") as XmlElement; if (slaves == null) { slaves = xmldoc.CreateElement("Slaves"); xmldoc.DocumentElement.AppendChild(slaves); } XmlElement slave = xmldoc.CreateElement("Slave"); XmlAttribute xa = xmldoc.CreateAttribute("index"); xa.InnerText = index.ToString(); slave.Attributes.Append(xa); slaves.AppendChild(slave); } public void AddSlaveItem(int index, string aname, string avalue) { string srch = "Config/Slaves/Slave[@index='" + index.ToString() + "']"; XmlElement slave = xmldoc.SelectSingleNode(srch) as XmlElement; if (slave != null) { XmlAttribute xa = xmldoc.CreateAttribute(aname); xa.InnerText = avalue; slave.Attributes.Append(xa); } } } }