KeyValue Config

public class ConfigHelper
    {
        public static ScriptsHelper Scripts
        {
            get { return new ScriptsHelper(); }
        }
        public static ParametersHelper Parameters
        {
            get { return new ParametersHelper(); }
        }
    }
    public class ScriptsHelper
    {
        string fileName = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"Scripts.xml");
        public PerScript this[string name]
        {
            get
            {
                string value = Tools.ExecuteXPathInFile(fileName, string.Format(@"/Scripts/Script[@name='{0}']/text()", name.Trim()));
                string connection = Tools.ExecuteXPathInFile(fileName, string.Format(@"/Scripts/Script[@name='{0}']/@connectionName", name.Trim()));
                PerScript script = new PerScript(value, connection);
                return script;
            }
        }

        public class PerScript
        {
            public PerScript(string value, string connection)
            {
                m_Value = value;
                m_Connection = connection;
            }

            string m_Value;
            public string Value { get { return m_Value; } }

            string m_Connection;
            public string Connection { get { return m_Connection; } }
        }
    }

    public class ParametersHelper
    {
        string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstValue.SERVICE_PARAMETER_FILE);
        public PerKeyValue this[string name]
        {
            get
            {
                string keyValueStr = Tools.ExecuteXPathInFile(fileName, string.Format(@"/KeyValues/KeyValue[@name='{0}']", name.Trim()));
                if (keyValueStr != null)
                {
                    PerKeyValue returnObj = new PerKeyValue(name, keyValueStr);
                    return returnObj;
                }
                else
                {
                    return null;
                }
            }
        }

        public class PerKeyValue
        {
            public PerKeyValue(string name, string keyValueStr)
            {
                m_KeyValueStr = keyValueStr;
                m_Name = name;
                m_Value = Tools.ExecuteXPathInString(keyValueStr, @"/KeyValue/text()");
            }

            string m_KeyValueStr;

            private string m_Name;
            public string Name { get { return m_Name; } }

            public PerKeyValueAttributes Attributes
            {
                get
                {
                    string[] temp = m_KeyValueStr.GetSubString(@"(?<=<KeyValues+).+.(?=/?>)");
                    if (temp != null && temp.Length > 0)
                        return new PerKeyValueAttributes(temp[0]);
                    else
                        return null;
                }
            }

            string m_Value;
            public string Value { get { return m_Value; } }

            public List<PerKeyValue> KeyValus
            {
                get
                {
                    List<PerKeyValue> list = null;
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(m_KeyValueStr);
                    XmlNodeList nodes = doc.SelectNodes("/KeyValue/KeyValue");
                    if (nodes != null && nodes.Count > 0)
                    {
                        list = new List<PerKeyValue>();
                        foreach (XmlNode item in nodes)
                        {
                            list.Add(new PerKeyValue(item.Attributes["name"].InnerText, item.OuterXml));
                        }
                    }
                    return list;
                }
            }

            public PerKeyValue this[string name]
            {
                get
                {
                    string keyValueStr = Tools.ExecuteXPathInString(m_KeyValueStr, string.Format(@"/KeyValue/KeyValue[@name='{0}']", name.Trim()));
                    if (keyValueStr != null)
                        return new PerKeyValue(name, keyValueStr);
                    else
                        return null;
                }
            }
        }

        public class PerKeyValueAttributes
        {
            public PerKeyValueAttributes(string attributes)
            {
                m_Attributes = attributes;
            }

            string m_Attributes = string.Empty;

            public string this[string name]
            {
                get
                {
                    string[] results = m_Attributes.GetSubString(string.Format(@"(?<={0}s*="").+", name));
                    if (results != null && results.Length > 0)
                    {
                        return results[0].ReplaceString(@""".*", string.Empty);
                    }
                    else
                        return null;
                }
            }
        }
    }

  

原文地址:https://www.cnblogs.com/Wolfmanlq/p/4227097.html