BaseUtil.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. namespace UAS_Web.tool
  7. {
  8. class BaseUtil
  9. {
  10. public static Object GetCacheData(string ParamName)
  11. {
  12. try
  13. {
  14. Object o = null;
  15. //根据地址读取xml文件
  16. XmlDocument doc = new XmlDocument();
  17. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  18. //忽略文档里面的注释
  19. settings.IgnoreComments = true;
  20. XmlReader reader = XmlReader.Create(System.AppDomain.CurrentDomain.BaseDirectory + "cache.xml", settings);
  21. doc.Load(reader);
  22. //先得到根节点
  23. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  24. //再由根节点去找制定的节点
  25. XmlNodeList nodeList = rootNode.ChildNodes;
  26. foreach (XmlNode node in nodeList)
  27. {
  28. //找到了这个节点名字
  29. if (node.Name == ParamName)
  30. {
  31. //返回节点的内容
  32. switch (((XmlElement)node).GetAttribute("Type"))
  33. {
  34. case "System.String":
  35. o = node.InnerText;
  36. break;
  37. case "System.Int32":
  38. o = int.Parse(node.InnerText);
  39. break;
  40. case "System.Boolean":
  41. o = node.InnerText == "True" ? true : false;
  42. break;
  43. default:
  44. break;
  45. }
  46. break;
  47. }
  48. }
  49. //关闭reader
  50. reader.Close();
  51. if (o == null)
  52. return "";
  53. else
  54. return o;
  55. }
  56. catch (Exception e)
  57. {
  58. return "";
  59. }
  60. }
  61. public static void SetCacheData(string ParamName, object Value)
  62. {
  63. try
  64. {
  65. //根据地址读取xml文件
  66. XmlDocument doc = new XmlDocument();
  67. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  68. //忽略文档里面的注释
  69. settings.IgnoreComments = true;
  70. XmlReader reader = XmlReader.Create(System.AppDomain.CurrentDomain.BaseDirectory + "cache.xml", settings);
  71. doc.Load(reader);
  72. //先得到根节点
  73. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  74. //再由根节点去找制定的节点
  75. XmlNodeList nodeList = rootNode.ChildNodes;
  76. bool flag = false;
  77. foreach (XmlNode node in nodeList)
  78. {
  79. //找到了这个节点名字
  80. if (node.Name == ParamName)
  81. {
  82. //就直接赋值
  83. node.InnerText = Value.ToString();
  84. flag = true;
  85. }
  86. }
  87. //如果没有该节点,就创建节点保存结果
  88. if (!flag)
  89. {
  90. //创建节点
  91. XmlElement newNode = doc.CreateElement(ParamName);
  92. XmlAttribute attr = doc.CreateAttribute("Type");
  93. attr.InnerText = Value.GetType().ToString();
  94. newNode.InnerText = Value.ToString();
  95. newNode.SetAttributeNode(attr);
  96. //讲新建的节点挂到根节点上
  97. rootNode.AppendChild(newNode);
  98. }
  99. //关闭Reader
  100. reader.Close();
  101. doc.Save(System.AppDomain.CurrentDomain.BaseDirectory + "cache.xml");
  102. }
  103. catch (Exception e)
  104. {
  105. }
  106. }
  107. }
  108. }