在开发中经常会用到各种kv类型的配置 文件,像这样的
<?xml version="1.0" encoding="utf-8" ?> <source> <item id="101" value="1125"/> <item id="102" value="413"/> <item id="103" value="181"/> <item id="104" value="1642"/> <item id="105" value="926"/> <item id="106" value="1088"/> <item id="107" value="1243"/> </source>
那么我们可以用一个辅助类来很好的解决哦
1 public class XMLSourceHelp 2 { 3 /// <summary> 4 /// XML数据文件数据列表 5 /// </summary> 6 private static Dictionary<EXMLDataSource, DataTable> XmlDataSourceList = null; 7 8 /// <summary> 9 /// XML数据文件枚举 10 /// </summary> 11 public enum EXMLDataSource 12 { 13 /// <summary> 14 /// ModuleTypeIconSource文件 15 /// </summary> 16 ModuleTypeIconSource = 0, 17 /// <summary> 18 /// ModuleTypeSource文件 19 /// </summary> 20 ModuleTypeSource = 1, 21 /// <summary> 22 /// SourceTypeFile文件 23 /// </summary> 24 SourceTypeFile = 2, 25 /// <summary> 26 /// FeedBack文件 27 /// </summary> 28 FeedBack = 3, 29 /// <summary> 30 /// QueryBracket文件 31 /// </summary> 32 QueryBracket = 4, 33 /// <summary> 34 /// QueryLogicOperator文件 35 /// </summary> 36 QueryLogicOperator = 5, 37 /// <summary> 38 /// QueryModule文件 39 /// </summary> 40 QueryModule = 6, 41 /// <summary> 42 /// QueryOperator文件 43 /// </summary> 44 QueryOperator = 7, 45 /// <summary> 46 /// QuerySelectModule文件 47 /// </summary> 48 QuerySelectModule = 8, 49 /// <summary> 50 /// 反射参数配置 51 /// </summary> 52 ParaConfig=9, 53 /// <summary> 54 /// 任务类型对应反射类型标记量 55 /// </summary> 56 SourceDllIndex = 10, 57 /// <summary> 58 /// 北京消息队列文件 59 /// </summary> 60 Lightweight=11 61 } 62 63 /// <summary> 64 /// 本类实例对象 65 /// </summary> 66 private static XMLSourceHelp m_sh; 67 68 /// <summary> 69 /// XML数据文件数据辅助类 70 /// </summary> 71 public static XMLSourceHelp SH 72 { 73 get 74 { 75 if (m_sh == null) 76 m_sh = new XMLSourceHelp(); 77 return XMLSourceHelp.m_sh; 78 } 79 private set { XMLSourceHelp.m_sh = value; } 80 } 81 82 /// <summary> 83 /// 功能描述:获取数据对象 84 /// 作 者:huangzh 85 /// 创建日期:2015-10-10 17:47:45 86 /// 任务编号: 87 /// </summary> 88 /// <param name="file">file</param> 89 /// <returns>返回值</returns> 90 public DataTable GetSource(EXMLDataSource file) 91 { 92 if (XmlDataSourceList == null) 93 return null; 94 DataTable dt = new DataTable(); 95 XmlDataSourceList.TryGetValue(file, out dt); 96 return dt.Copy(); 97 } 98 99 /// <summary> 100 /// 功能描述:加载数据 101 /// 作 者:huangzh 102 /// 创建日期:2015-10-10 17:48:32 103 /// 任务编号: 104 /// </summary> 105 public void LoadSource() 106 { 107 if (XmlDataSourceList == null) 108 { 109 string strServerPath = AppDomain.CurrentDomain.BaseDirectory; 110 XmlDataSourceList = new Dictionary<EXMLDataSource, DataTable>(); 111 foreach (EXMLDataSource item in Enum.GetValues(typeof(EXMLDataSource))) 112 { 113 XmlDataSourceList.Add(item, LoadXmlInfo(strServerPath + "Data\\" + item + ".xml")); 114 } 115 } 116 } 117 118 /// <summary> 119 /// 转换数字码为中文名 120 /// </summary> 121 /// <param name="objId">Id数字码</param> 122 /// <param name="item">文件</param> 123 /// <returns>返回转换后的名字</returns> 124 public string ConvertIdToName(object objId, EXMLDataSource file) 125 { 126 if (objId == null || string.IsNullOrWhiteSpace(objId.ToString())) 127 { 128 return string.Empty; 129 } 130 131 DataTable dtSource = GetSource(file); 132 133 if (dtSource == null) 134 { 135 return string.Empty; 136 } 137 var names = from item in dtSource.AsEnumerable() 138 where item.Field<string>("id") == objId.ToString() 139 select item.Field<string>("value"); 140 if (names == null || names.Count() <= 0) 141 return ""; 142 return names.First(); 143 } 144 145 /// <summary> 146 /// 功能描述:构造方法 147 /// 作 者:huangzh 148 /// 创建日期:2015-09-25 09:09:26 149 /// 任务编号: 150 /// </summary> 151 private XMLSourceHelp() 152 { 153 } 154 155 /// <summary> 156 /// 加载XML信息 157 /// </summary> 158 /// <param name="strPath">文件路径</param> 159 /// <returns>返回Xml信息数据表</returns> 160 private DataTable LoadXmlInfo(string strPath) 161 { 162 DataTable dt = CreateDt(); 163 GetSourceByFile(strPath, ref dt); 164 return dt; 165 } 166 167 /// <summary> 168 /// 功能描述:读取数据 169 /// 作 者:huangzh 170 /// 创建日期:2015-09-25 09:28:43 171 /// 任务编号: 172 /// </summary> 173 /// <param name="strFile">strFile</param> 174 /// <param name="dt">dt</param> 175 private void GetSourceByFile(string strFile, ref DataTable dt) 176 { 177 XmlDocument document = new XmlDocument(); 178 document.Load(strFile); 179 XmlNodeList nodelist = document.SelectSingleNode("/source").ChildNodes; 180 foreach (XmlNode xn in nodelist) 181 { 182 if (xn.NodeType == XmlNodeType.Element) 183 { 184 DataRow dr = dt.NewRow(); 185 dr[0] = xn.Attributes["id"].Value; 186 dr[1] = xn.Attributes["value"].Value; 187 dt.Rows.Add(dr); 188 } 189 } 190 } 191 192 /// <summary> 193 /// 功能描述:创建一个DataTable 194 /// 作 者:huangzh 195 /// 创建日期:2015-09-25 09:28:08 196 /// 任务编号: 197 /// </summary> 198 /// <returns>返回值</returns> 199 private DataTable CreateDt() 200 { 201 DataTable dt = new DataTable(); 202 dt.Columns.Add("id", typeof(string)); 203 dt.Columns.Add("value", typeof(string)); 204 return dt; 205 } 206 207 208 /// <summary> 209 /// 功能描述:返回指定ID的对应序列号 210 /// 作 者:huangzh 211 /// 创建日期:2015-10-12 17:34:40 212 /// 任务编号: 213 /// </summary> 214 /// <param name="objid">objid</param> 215 /// <param name="EXMLDataSource">文件</param> 216 /// <returns>返回值</returns> 217 public int GetIndexById(object objid, EXMLDataSource file) 218 { 219 DataTable dtSource = GetSource(file); 220 return ZhuoYueE.Dop.Web.Base.ProEnv.GetIndexInTableByField(dtSource, "id", objid.ToString()); 221 } 222 223 /// <summary> 224 /// 功能描述:返回指定Value的对应的第一个序列号 225 /// 作 者:huangzh 226 /// 创建日期:2015-10-12 17:35:50 227 /// 任务编号: 228 /// </summary> 229 /// <param name="strValue">strValue</param> 230 /// <param name="t">t</param> 231 /// <returns>返回值</returns> 232 public int GetIndexByValue(string strValue, EXMLDataSource file) 233 { 234 DataTable dtSource = GetSource(file); 235 return ZhuoYueE.Dop.Web.Base.ProEnv.GetIndexInTableByField(dtSource, "value", strValue); 236 } 237 238 /// <summary> 239 /// 修改XML的一个键值 240 /// </summary> 241 /// <param name="objid">objid</param> 242 /// <param name="strValue">值</param> 243 /// <param name="file">文件</param> 244 public void ModifyValueById(object objid, string strValue, EXMLDataSource file) 245 { 246 string strServerPath = AppDomain.CurrentDomain.BaseDirectory; 247 strServerPath = strServerPath.Substring(0, strServerPath.Length - 1); 248 var names = from item in GetSource(file).AsEnumerable() 249 where item.Field<string>("id") == objid.ToString() 250 select item.Field<string>("value"); 251 if (names != null && names.Count() == 1) 252 { 253 XmlDocument document = new XmlDocument(); 254 document.Load(strServerPath + "\\data\\" + file + ".xml"); 255 XmlNodeList nodelist = document.SelectSingleNode("/source").ChildNodes; 256 foreach (XmlNode xn in nodelist) 257 { 258 if (xn.NodeType == XmlNodeType.Element) 259 { 260 if (xn.Attributes["id"].Value == objid.ToString()) 261 { 262 xn.Attributes["value"].Value = strValue; 263 break; 264 } 265 } 266 } 267 document.Save(strServerPath + "\\data\\" + file + ".xml"); 268 } 269 else 270 { 271 272 XmlDocument document = new XmlDocument(); 273 document.Load(strServerPath + "\\data\\" + file + ".xml"); 274 XmlNode xn = document.SelectSingleNode("/source"); 275 XmlNode xnNew = document.CreateNode(XmlNodeType.Element, "item", null); 276 XmlAttribute attId = document.CreateAttribute("id"); 277 attId.Value = objid.ToString(); 278 XmlAttribute attvalue = document.CreateAttribute("value"); 279 attvalue.Value = strValue; 280 xnNew.Attributes.Append(attId); 281 xnNew.Attributes.Append(attvalue); 282 xn.AppendChild(xnNew); 283 document.Save(strServerPath + "\\data\\" + file + ".xml"); 284 } 285 if (XmlDataSourceList.ContainsKey(file)) 286 { 287 XmlDataSourceList[file] = LoadXmlInfo(strServerPath + "\\Data\\" + file + ".xml"); 288 } 289 else 290 { 291 XmlDataSourceList.Add(file, LoadXmlInfo(strServerPath + "\\Data\\" + file + ".xml")); 292 } 293 } 294 }
其中“EXMLDataSource”就是配置文件名的枚举了。
这个辅助类提供增改查功能,使用方便。只需要这么调用就可以了
XMLSourceHelp.SH.ConvertIdToName("101", XMLSourceHelp.EXMLDataSource.Lightweight);
时间: 2024-10-15 20:56:30