using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml; using UAS_MES_NEW.PublicMethod; namespace UAS_Tools_HY.PublicMethods { internal class BaseUtil { public static Dictionary ToDictionary(string JsonData) { object Data = null; Dictionary Dic = new Dictionary(); if (JsonData.StartsWith("[")) { //如果目标直接就为数组类型,则将会直接输出一个Key为List的List>集合 //使用示例List> ListDic = (List>)Dic["List"]; List> List = new List>(); MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组 foreach (Match ListItem in ListMatch) { List.Add(ToDictionary(ListItem.ToString()));//递归调用 } Data = List; Dic.Add("List", Data); } else { MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值 foreach (Match item in Match) { try { if (item.Groups[2].ToString().StartsWith("[")) { //如果目标是数组,将会输出一个Key为当前Json的List>集合 //使用示例List> ListDic = (List>)Dic["Json中的Key"]; List> List = new List>(); MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组 foreach (Match ListItem in ListMatch) { List.Add(ToDictionary(ListItem.ToString()));//递归调用 } Data = List; } else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入 Dic.Add(item.Groups[1].ToString(), Data); } catch { } } } return Dic; } public static void SetCacheData(string ParamName, object Value) { try { //根据地址读取xml文件 XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false }; //忽略文档里面的注释 settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(SystemCacheClass.CacheFilePath, settings); doc.Load(reader); //先得到根节点 XmlNode rootNode = doc.SelectSingleNode("cacheInfo"); //再由根节点去找制定的节点 XmlNodeList nodeList = rootNode.ChildNodes; bool flag = false; foreach (XmlNode node in nodeList) { //找到了这个节点名字 if (node.Name == ParamName) { //就直接赋值 node.InnerText = Value.ToString(); flag = true; } } //如果没有该节点,就创建节点保存结果 if (!flag) { //创建节点 XmlElement newNode = doc.CreateElement(ParamName); XmlAttribute attr = doc.CreateAttribute("Type"); attr.InnerText = Value.GetType().ToString(); newNode.InnerText = Value.ToString(); newNode.SetAttributeNode(attr); //讲新建的节点挂到根节点上 rootNode.AppendChild(newNode); } //关闭Reader reader.Close(); doc.Save(SystemCacheClass.CacheFilePath); } catch (Exception e) { LogManager.DoLog(e.Message); } } public static Object GetCacheData(string ParamName) { try { Object o = null; //根据地址读取xml文件 XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false }; //忽略文档里面的注释 settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(SystemCacheClass.CacheFilePath, settings); doc.Load(reader); //先得到根节点 XmlNode rootNode = doc.SelectSingleNode("cacheInfo"); //再由根节点去找制定的节点 XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { //找到了这个节点名字 if (node.Name == ParamName) { //返回节点的内容 switch (((XmlElement)node).GetAttribute("Type")) { case "System.String": o = node.InnerText; break; case "System.Int32": o = int.Parse(node.InnerText); break; case "System.Boolean": o = node.InnerText == "True" ? true : false; break; default: break; } break; } } //关闭reader reader.Close(); if (o == null) return ""; else return o; } catch (Exception e) { LogManager.DoLog(e.Message); return ""; } } } }