XMLDataEditor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Xml;
  6. using XRTool.Util;
  7. /// <summary>
  8. /// 一个xml编辑器
  9. /// </summary>
  10. namespace PublicTools.XMLDataBase
  11. {
  12. /// <summary>
  13. /// 针对某个xml文件进行编辑的类
  14. /// </summary>
  15. public class XMLDataEditor : TableInterface
  16. {
  17. private XmlDocument xmlDoc; //可操作的xml文档
  18. private XmlNode xmlRootNode; //根节点
  19. /// <summary>
  20. /// 文件的绝对路径
  21. /// </summary>
  22. private string filePath;
  23. /// <summary>
  24. /// 表格的名称
  25. /// </summary>
  26. private string tableName;
  27. /// <summary>
  28. /// 根节点的名称
  29. /// </summary>
  30. /// <summary>
  31. /// 以绝对路径创建一个对象
  32. /// </summary>
  33. /// <param name="filePath"></param>
  34. public XMLDataEditor(string filePath)
  35. {
  36. this.filePath = filePath;
  37. }
  38. public XMLDataEditor()
  39. {
  40. }
  41. public bool Open()
  42. {
  43. return Open(filePath);
  44. }
  45. /// <summary>
  46. /// 打开xml表格
  47. /// </summary>
  48. /// <param name="path"></param>
  49. /// <returns></returns>
  50. public bool Open(string path)
  51. {
  52. if (xmlDoc != null && !string.IsNullOrEmpty(this.filePath))
  53. {
  54. return true;
  55. }
  56. ///开启之前先关闭
  57. //Close();
  58. if (File.Exists(path))
  59. {
  60. xmlDoc = new XmlDocument();
  61. tableName = Path.GetFileNameWithoutExtension(path);
  62. try
  63. {
  64. xmlDoc.Load(path);
  65. xmlRootNode = xmlDoc.SelectSingleNode(tableName);
  66. return xmlRootNode != null;
  67. }
  68. catch (Exception ex)
  69. {
  70. UnityLog.LogError(path + ex.ToString());
  71. }
  72. }
  73. return false;
  74. }
  75. public void Close()
  76. {
  77. if (xmlDoc != null && !string.IsNullOrEmpty(filePath))
  78. {
  79. try
  80. {
  81. xmlDoc.Save(filePath);
  82. xmlDoc = null;
  83. //filePath = null;
  84. }
  85. catch (Exception ex)
  86. {
  87. UnityLog.LogError(filePath + "\n" + ex.ToString());
  88. }
  89. }
  90. }
  91. public bool InsertData<T>(T data, string priKey)
  92. {
  93. Dictionary<string, string> field = ClazzFactory.ClazzAnalyze(data);
  94. if (field != null && field.Count > 0)
  95. {
  96. XmlNode table = xmlRootNode;
  97. if (string.IsNullOrEmpty(priKey))
  98. {
  99. foreach (var item in field)
  100. {
  101. priKey = item.Value;
  102. break;
  103. }
  104. }
  105. return InsertData<T>(data, table, field, priKey);
  106. }
  107. return false;
  108. }
  109. /// <summary>
  110. /// 插入数据
  111. /// </summary>
  112. /// <typeparam name="T"></typeparam>
  113. /// <param name="table"></param>
  114. /// <param name="field"></param>
  115. private bool InsertData<T>(T data, XmlNode table, Dictionary<string, string> field, string priKey)
  116. {
  117. try
  118. {
  119. if (string.IsNullOrEmpty(priKey) || priKey == "null")
  120. {
  121. return false;
  122. }
  123. if (table.SelectSingleNode(priKey) != null)
  124. {
  125. return UpdateData<T>(data, table, field, priKey);
  126. }
  127. ///添加一条数据
  128. ///按照key值添加一个节点元素
  129. XmlElement element = xmlDoc.CreateElement(priKey);
  130. ///循环遍历变量集合,将类的数据集合起来添加
  131. foreach (var item in field)
  132. {
  133. XmlAttribute xa = xmlDoc.CreateAttribute(item.Key);
  134. xa.Value = item.Value;
  135. element.Attributes.Append(xa);
  136. }
  137. table.AppendChild(element);
  138. return true;
  139. }
  140. catch (Exception ex)
  141. {
  142. UnityLog.LogError(data + " " + priKey + ex.ToString());
  143. Close();
  144. }
  145. return false;
  146. }
  147. public bool InsertData<T>(T data)
  148. {
  149. return InsertData<T>(data, null);
  150. }
  151. /// <summary>
  152. /// 尝试获取xml的一个表,如果此xml没有此表,则创建一个
  153. /// </summary>
  154. /// <param name="tableName"></param>
  155. /// <returns></returns>
  156. public XmlNode TryGetTable(string tableName)
  157. {
  158. XmlNode node = xmlRootNode.SelectSingleNode(tableName);
  159. if (node != null)
  160. {
  161. return node;
  162. }
  163. return CreateTable(tableName);
  164. }
  165. /// <summary>
  166. /// 创建一个子节点
  167. /// </summary>
  168. /// <param name="tableName"></param>
  169. public XmlNode CreateTable(string tableName)
  170. {
  171. XmlNode table = xmlDoc.CreateElement(tableName);
  172. xmlRootNode.AppendChild(table);
  173. Save();
  174. return table;
  175. }
  176. /// <summary>
  177. ///
  178. /// </summary>
  179. /// <typeparam name="T"></typeparam>
  180. /// <param name="priKey"></param>
  181. /// <returns></returns>
  182. public T FindData<T>(string priKey)
  183. {
  184. T t = default(T);
  185. XmlNode table = xmlRootNode;
  186. try
  187. {
  188. XmlNode obj = table.SelectSingleNode(priKey);
  189. if (obj != null)
  190. {
  191. Dictionary<string, string> dic = new Dictionary<string, string>();
  192. for (int i = 0; i < obj.Attributes.Count; i++)
  193. {
  194. dic.Add(obj.Attributes[i].LocalName, obj.Attributes[i].Value);
  195. }
  196. t = ClazzFactory.ClazzPack<T>(dic);
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. Close();
  202. UnityLog.LogError(priKey + ex.ToString());
  203. }
  204. return t;
  205. }
  206. public List<T> FindAllData<T>()
  207. {
  208. XmlNode table = xmlRootNode;
  209. if (table == null || table.ChildNodes.Count == 0)
  210. {
  211. return null;
  212. }
  213. List<T> list = new List<T>();
  214. for (int i = 0; i < table.ChildNodes.Count; i++)
  215. {//遍历表的子节点
  216. Dictionary<string, string> values = new Dictionary<string, string>();
  217. for (int j = 0; j < table.ChildNodes[i].Attributes.Count; j++)
  218. {
  219. values.Add(table.ChildNodes[i].Attributes[j].LocalName, table.ChildNodes[i].Attributes[j].Value);
  220. }
  221. list.Add(ClazzFactory.ClazzPack<T>(values));
  222. }
  223. return list;
  224. }
  225. public List<T> FindAllData<T>(string data)
  226. {
  227. return null;
  228. }
  229. /// <summary>
  230. /// 查找所有对象
  231. /// </summary>
  232. /// <typeparam name="T"></typeparam>
  233. /// <returns></returns>
  234. public List<T> FindAllData<T>(byte[] data)
  235. {
  236. try
  237. {
  238. xmlDoc = new XmlDocument();
  239. string xmlStr = UnityUtil.GetUTF8String(data);
  240. xmlDoc.LoadXml(xmlStr);
  241. tableName = typeof(T).Name;
  242. xmlRootNode = xmlDoc.SelectSingleNode(tableName);
  243. }
  244. catch (Exception ex)
  245. {
  246. UnityLog.LogError(ex.ToString());
  247. }
  248. return FindAllData<T>();
  249. }
  250. public bool UpdateData<T>(T data, string priKey)
  251. {
  252. Dictionary<string, string> field = ClazzFactory.ClazzAnalyze(data);
  253. if (field != null && field.Count > 0)
  254. {
  255. XmlNode table = xmlRootNode;
  256. if (string.IsNullOrEmpty(priKey))
  257. {
  258. foreach (var item in field)
  259. {
  260. priKey = item.Value;
  261. break;
  262. }
  263. }
  264. return UpdateData<T>(data, table, field, priKey);
  265. }
  266. return false;
  267. }
  268. private bool UpdateData<T>(T data, XmlNode table, Dictionary<string, string> field, string priKey)
  269. {
  270. try
  271. {
  272. XmlNode obj = table.SelectSingleNode(priKey);
  273. if (obj == null)
  274. {
  275. //待更新数据不存在,更新失败
  276. return false;
  277. }
  278. ///需要判断变量名是否发生了变化
  279. bool isChanged = false;
  280. if (obj.Attributes.Count != field.Count)
  281. {
  282. isChanged = true;
  283. }
  284. if (!isChanged)
  285. {
  286. int index = 0;
  287. foreach (var item in field)
  288. {
  289. ///变量名不一致
  290. if (obj.Attributes[index].LocalName != item.Key)
  291. {
  292. isChanged = true;
  293. break;
  294. }
  295. index++;
  296. }
  297. }
  298. if (isChanged)
  299. {
  300. if (DeleteData<T>(priKey))
  301. {
  302. return InsertData(data, priKey);
  303. }
  304. else
  305. {
  306. return false;
  307. }
  308. }
  309. else
  310. {
  311. foreach (var item in field)
  312. {
  313. if (obj.Attributes[item.Key].Value != item.Value)
  314. {
  315. obj.Attributes[item.Key].Value = item.Value;
  316. isChanged = true;
  317. }
  318. }
  319. }
  320. return isChanged;
  321. }
  322. catch (Exception ex)
  323. {
  324. UnityLog.LogError(data + " " + priKey + ex.ToString());
  325. Close();
  326. }
  327. return false;
  328. }
  329. public bool UpdateData<T>(T data)
  330. {
  331. return UpdateData<T>(data, null);
  332. }
  333. public bool DeleteData<T>(string key)
  334. {
  335. XmlNode table = xmlRootNode;
  336. try
  337. {
  338. XmlNode obj = table.SelectSingleNode(key);//按照key值确定元素对象
  339. if (obj == null)
  340. {
  341. return false;
  342. }
  343. obj.RemoveAll();
  344. table.RemoveChild(obj);
  345. return true;
  346. }
  347. catch (Exception ex)
  348. {
  349. UnityLog.LogError(ex.ToString());
  350. }
  351. return false;
  352. }
  353. public bool DeleteAllData<T>()
  354. {
  355. try
  356. {
  357. XmlNode table = xmlRootNode;
  358. table.RemoveAll();
  359. return true;
  360. }
  361. catch (Exception ex)
  362. {
  363. UnityLog.LogError(ex.ToString());
  364. }
  365. return false;
  366. }
  367. /// <summary>
  368. /// 建表
  369. /// </summary>
  370. /// <param name="tableName"></param>
  371. /// <returns></returns>
  372. public bool Create(string tableName)
  373. {
  374. try
  375. {
  376. FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
  377. if (xmlDoc == null)
  378. {
  379. xmlDoc = new XmlDocument();
  380. }
  381. this.tableName = Path.GetFileNameWithoutExtension(tableName);
  382. XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
  383. xmlRootNode = xmlDoc.CreateElement(this.tableName);
  384. xmlDoc.AppendChild(xmlDeclaration);
  385. xmlDoc.AppendChild(xmlRootNode);
  386. fs.Dispose();
  387. fs.Close();
  388. return xmlRootNode != null;
  389. }
  390. catch (Exception ex)
  391. {
  392. UnityLog.Log("Create Error" + this.tableName + ex.ToString());
  393. }
  394. return false;
  395. }
  396. public void Save()
  397. {
  398. if (xmlDoc != null && !string.IsNullOrEmpty(filePath))
  399. {
  400. try
  401. {
  402. xmlDoc.Save(filePath);
  403. }
  404. catch (Exception ex)
  405. {
  406. UnityLog.LogError(filePath + "\n" + ex.ToString());
  407. }
  408. }
  409. }
  410. }
  411. }