使用xml保存KV配置信息操作类

前端之家收集整理的这篇文章主要介绍了使用xml保存KV配置信息操作类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

主要用于配置的读取修改等,方便统一化操作,可用于bs或cs代码里面的枚举为文件名,xml文件格式为 id value的格式,使用简单,一看就会

  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. Type1 = 1,Type2 = 2
  14. }
  15.  
  16. /// <summary>
  17. /// 本类实例对象
  18. /// </summary>
  19. private static XMLSourceHelp m_sh;
  20.  
  21. /// <summary>
  22. /// XML数据文件数据辅助类
  23. /// </summary>
  24. public static XMLSourceHelp SH
  25. {
  26. get
  27. {
  28. if (m_sh == null)
  29. m_sh = new XMLSourceHelp();
  30. return XMLSourceHelp.m_sh;
  31. }
  32. private set { XMLSourceHelp.m_sh = value; }
  33. }
  34.  
  35. /// <summary>
  36. /// 功能描述:获取数据对象
  37. /// 作  者: 爱给模板网 2gei.cn
  38. /// 创建日期:2015-10-10 17:47:45
  39. /// 任务编号:
  40. /// </summary>
  41. /// <param name="file">file</param>
  42. /// <returns>返回值</returns>
  43. public DataTable GetSource(EXMLDataSource file)
  44. {
  45. if (XmlDataSourceList == null)
  46. return null;
  47. DataTable dt = new DataTable();
  48. XmlDataSourceList.TryGetValue(file,out dt);
  49. return dt.Copy();
  50. }
  51.  
  52. /// <summary>
  53. /// 功能描述:加载数据
  54. /// 作  者: 爱给模板网 2gei.cn
  55. /// 创建日期:2015-10-10 17:48:32
  56. /// 任务编号:
  57. /// </summary>
  58. public void LoadSource()
  59. {
  60. if (XmlDataSourceList == null)
  61. {
  62. string strServerPath = AppDomain.CurrentDomain.BaseDirectory;
  63. XmlDataSourceList = new Dictionary<EXMLDataSource,DataTable>();
  64. foreach (EXMLDataSource item in Enum.GetValues(typeof(EXMLDataSource)))
  65. {
  66. XmlDataSourceList.Add(item,LoadXmlInfo(strServerPath + "Data\\" + item + ".xml"));
  67. }
  68. }
  69. }
  70.  
  71. /// <summary>
  72. /// 转换ID为中文
  73. /// </summary>
  74. /// <param name="objId">Id数字码</param>
  75. /// <param name="file">文件</param>
  76. /// <param name="strDefualtValue">默认值</param>
  77. /// <returns>返回转换后的名字</returns>
  78. public string ConvertIdToName(object objId,EXMLDataSource file,string strDefualtValue = "")
  79. {
  80. if (objId == null || string.IsNullOrWhiteSpace(objId.ToString()))
  81. {
  82. return strDefualtValue;
  83. }
  84.  
  85. DataTable dtSource = GetSource(file);
  86.  
  87. if (dtSource == null)
  88. {
  89. return strDefualtValue;
  90. }
  91. var names = from item in dtSource.AsEnumerable()
  92. where item.Field<string>("id") == objId.ToString()
  93. select item.Field<string>("value");
  94. if (names == null || names.Count() <= 0)
  95. return strDefualtValue;
  96. return names.First();
  97. }
  98.  
  99. /// <summary>
  100. /// 功能描述:构造方法
  101. /// 作  者: 爱给模板网 2gei.cn
  102. /// 创建日期:2015-09-25 09:09:26
  103. /// 任务编号:
  104. /// </summary>
  105. private XMLSourceHelp()
  106. {
  107. }
  108.  
  109. /// <summary>
  110. /// 加载XML信息
  111. /// </summary>
  112. /// <param name="strPath">文件路径</param>
  113. /// <returns>返回Xml信息数据表</returns>
  114. private DataTable LoadXmlInfo(string strPath)
  115. {
  116. DataTable dt = CreateDt();
  117. GetSourceByFile(strPath,ref dt);
  118. return dt;
  119. }
  120.  
  121. /// <summary>
  122. /// 功能描述:读取数据
  123. /// 作  者: 爱给模板网 2gei.cn
  124. /// 创建日期:2015-09-25 09:28:43
  125. /// 任务编号:
  126. /// </summary>
  127. /// <param name="strFile">strFile</param>
  128. /// <param name="dt">dt</param>
  129. private void GetSourceByFile(string strFile,ref DataTable dt)
  130. {
  131. XmlDocument document = new XmlDocument();
  132. document.Load(strFile);
  133. XmlNodeList nodelist = document.SelectSingleNode("/source").ChildNodes;
  134. foreach (XmlNode xn in nodelist)
  135. {
  136. if (xn.NodeType == XmlNodeType.Element)
  137. {
  138. DataRow dr = dt.NewRow();
  139. dr[0] = xn.Attributes["id"].Value;
  140. dr[1] = xn.Attributes["value"].Value;
  141. dt.Rows.Add(dr);
  142. }
  143. }
  144. }
  145.  
  146. /// <summary>
  147. /// 功能描述:创建一个DataTable
  148. /// 作  者: 爱给模板网 2gei.cn
  149. /// 创建日期:2015-09-25 09:28:08
  150. /// 任务编号:
  151. /// </summary>
  152. /// <returns>返回值</returns>
  153. private DataTable CreateDt()
  154. {
  155. DataTable dt = new DataTable();
  156. dt.Columns.Add("id",typeof(string));
  157. dt.Columns.Add("value",typeof(string));
  158. return dt;
  159. }
  160.  
  161.  
  162. /// <summary>
  163. /// 功能描述:返回指定ID的对应序列号
  164. /// 作  者: 爱给模板网 2gei.cn
  165. /// 创建日期:2015-10-12 17:34:40
  166. /// 任务编号:
  167. /// </summary>
  168. /// <param name="objid">objid</param>
  169. /// <param name="EXMLDataSource">文件</param>
  170. /// <returns>返回值</returns>
  171. public int GetIndexById(object objid,EXMLDataSource file)
  172. {
  173. DataTable dtSource = GetSource(file);
  174. return ZhuoYueE.Dop.Web.Base.ProEnv.GetIndexInTableByField(dtSource,"id",objid.ToString());
  175. }
  176.  
  177. /// <summary>
  178. /// 功能描述:返回指定Value的对应的第一个序列号
  179. /// 作  者: 爱给模板网 2gei.cn
  180. /// 创建日期:2015-10-12 17:35:50
  181. /// 任务编号:
  182. /// </summary>
  183. /// <param name="strValue">strValue</param>
  184. /// <param name="t">t</param>
  185. /// <returns>返回值</returns>
  186. public int GetIndexByValue(string strValue,"value",strValue);
  187. }
  188.  
  189. /// <summary>
  190. /// 修改XML的一个键值
  191. /// </summary>
  192. /// <param name="objid">objid</param>
  193. /// <param name="strValue">值</param>
  194. /// <param name="file">文件</param>
  195. public void ModifyValueById(object objid,string strValue,EXMLDataSource file)
  196. {
  197. string strServerPath = AppDomain.CurrentDomain.BaseDirectory;
  198. strServerPath = strServerPath.Substring(0,strServerPath.Length - 1);
  199. var names = from item in GetSource(file).AsEnumerable()
  200. where item.Field<string>("id") == objid.ToString()
  201. select item.Field<string>("value");
  202. if (names != null && names.Count() == 1)
  203. {
  204. XmlDocument document = new XmlDocument();
  205. document.Load(strServerPath + "\\data\\" + file + ".xml");
  206. XmlNodeList nodelist = document.SelectSingleNode("/source").ChildNodes;
  207. foreach (XmlNode xn in nodelist)
  208. {
  209. if (xn.NodeType == XmlNodeType.Element)
  210. {
  211. if (xn.Attributes["id"].Value == objid.ToString())
  212. {
  213. xn.Attributes["value"].Value = strValue;
  214. break;
  215. }
  216. }
  217. }
  218. document.Save(strServerPath + "\\data\\" + file + ".xml");
  219. }
  220. else
  221. {
  222.  
  223. XmlDocument document = new XmlDocument();
  224. document.Load(strServerPath + "\\data\\" + file + ".xml");
  225. XmlNode xn = document.SelectSingleNode("/source");
  226. XmlNode xnNew = document.CreateNode(XmlNodeType.Element,"item",null);
  227. XmlAttribute attId = document.CreateAttribute("id");
  228. attId.Value = objid.ToString();
  229. XmlAttribute attvalue = document.CreateAttribute("value");
  230. attvalue.Value = strValue;
  231. xnNew.Attributes.Append(attId);
  232. xnNew.Attributes.Append(attvalue);
  233. xn.AppendChild(xnNew);
  234. document.Save(strServerPath + "\\data\\" + file + ".xml");
  235. }
  236. if (XmlDataSourceList.ContainsKey(file))
  237. {
  238. XmlDataSourceList[file] = LoadXmlInfo(strServerPath + "\\Data\\" + file + ".xml");
  239. }
  240. else
  241. {
  242. XmlDataSourceList.Add(file,LoadXmlInfo(strServerPath + "\\Data\\" + file + ".xml"));
  243. }
  244. }
  245. }
  246.  



猜你在找的XML相关文章