BaseUtil.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using UAS_MES_NEW.PublicMethod;
  9. namespace UAS_Tools_HY.PublicMethods
  10. {
  11. internal class BaseUtil
  12. {
  13. public static Dictionary<string, object> ToDictionary(string JsonData)
  14. {
  15. object Data = null;
  16. Dictionary<string, object> Dic = new Dictionary<string, object>();
  17. if (JsonData.StartsWith("["))
  18. {
  19. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  20. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  21. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  22. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  23. foreach (Match ListItem in ListMatch)
  24. {
  25. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  26. }
  27. Data = List;
  28. Dic.Add("List", Data);
  29. }
  30. else
  31. {
  32. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  33. foreach (Match item in Match)
  34. {
  35. try
  36. {
  37. if (item.Groups[2].ToString().StartsWith("["))
  38. {
  39. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  40. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  41. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  42. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  43. foreach (Match ListItem in ListMatch)
  44. {
  45. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  46. }
  47. Data = List;
  48. }
  49. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  50. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  51. Dic.Add(item.Groups[1].ToString(), Data);
  52. }
  53. catch { }
  54. }
  55. }
  56. return Dic;
  57. }
  58. public static void SetCacheData(string ParamName, object Value)
  59. {
  60. try
  61. {
  62. //根据地址读取xml文件
  63. XmlDocument doc = new XmlDocument();
  64. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  65. //忽略文档里面的注释
  66. settings.IgnoreComments = true;
  67. XmlReader reader = XmlReader.Create(SystemCacheClass.CacheFilePath, settings);
  68. doc.Load(reader);
  69. //先得到根节点
  70. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  71. //再由根节点去找制定的节点
  72. XmlNodeList nodeList = rootNode.ChildNodes;
  73. bool flag = false;
  74. foreach (XmlNode node in nodeList)
  75. {
  76. //找到了这个节点名字
  77. if (node.Name == ParamName)
  78. {
  79. //就直接赋值
  80. node.InnerText = Value.ToString();
  81. flag = true;
  82. }
  83. }
  84. //如果没有该节点,就创建节点保存结果
  85. if (!flag)
  86. {
  87. //创建节点
  88. XmlElement newNode = doc.CreateElement(ParamName);
  89. XmlAttribute attr = doc.CreateAttribute("Type");
  90. attr.InnerText = Value.GetType().ToString();
  91. newNode.InnerText = Value.ToString();
  92. newNode.SetAttributeNode(attr);
  93. //讲新建的节点挂到根节点上
  94. rootNode.AppendChild(newNode);
  95. }
  96. //关闭Reader
  97. reader.Close();
  98. doc.Save(SystemCacheClass.CacheFilePath);
  99. }
  100. catch (Exception e)
  101. {
  102. LogManager.DoLog(e.Message);
  103. }
  104. }
  105. public static Object GetCacheData(string ParamName)
  106. {
  107. try
  108. {
  109. Object o = null;
  110. //根据地址读取xml文件
  111. XmlDocument doc = new XmlDocument();
  112. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  113. //忽略文档里面的注释
  114. settings.IgnoreComments = true;
  115. XmlReader reader = XmlReader.Create(SystemCacheClass.CacheFilePath, settings);
  116. doc.Load(reader);
  117. //先得到根节点
  118. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  119. //再由根节点去找制定的节点
  120. XmlNodeList nodeList = rootNode.ChildNodes;
  121. foreach (XmlNode node in nodeList)
  122. {
  123. //找到了这个节点名字
  124. if (node.Name == ParamName)
  125. {
  126. //返回节点的内容
  127. switch (((XmlElement)node).GetAttribute("Type"))
  128. {
  129. case "System.String":
  130. o = node.InnerText;
  131. break;
  132. case "System.Int32":
  133. o = int.Parse(node.InnerText);
  134. break;
  135. case "System.Boolean":
  136. o = node.InnerText == "True" ? true : false;
  137. break;
  138. default:
  139. break;
  140. }
  141. break;
  142. }
  143. }
  144. //关闭reader
  145. reader.Close();
  146. if (o == null)
  147. return "";
  148. else
  149. return o;
  150. }
  151. catch (Exception e)
  152. {
  153. LogManager.DoLog(e.Message);
  154. return "";
  155. }
  156. }
  157. }
  158. }