BaseUtil.cs 7.9 KB

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