KeyValue Config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 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( @"(?<=\<KeyValue\s+).+.(?=/?\>)" ); 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 ; } } } } |
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力!
欢迎您持续关注我的博客:)
版权所有,欢迎保留原文链接进行转载:)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步