BaseUtil.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. namespace UAS_XmlAnalysor
  5. {
  6. class BaseUtil
  7. {
  8. public static void CreateXml(string iSN, string iTestResult)
  9. {
  10. //创建XML文档
  11. FileStream fcaches = new FileStream(iSN ,FileMode.OpenOrCreate, FileAccess.ReadWrite);
  12. fcaches.Close();
  13. FileInfo info = new FileInfo(iSN);
  14. if (info.Length == 0)
  15. {
  16. XmlDocument doc = new XmlDocument();
  17. //创建类型声明节点
  18. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  19. doc.AppendChild(node);
  20. //创建根节点
  21. XmlElement xeRoot = doc.CreateElement("CacheInf");
  22. doc.AppendChild(xeRoot);
  23. doc.Save(iSN + ".xml");
  24. }
  25. }
  26. public static object GetCacheData(string ParamName)
  27. {
  28. try
  29. {
  30. object returnData = null;
  31. //根据地址读取xml文件
  32. XmlDocument doc = new XmlDocument();
  33. XmlReaderSettings settings = new XmlReaderSettings();
  34. //忽略文档里面的注释
  35. settings.IgnoreComments = true;
  36. XmlReader reader = XmlReader.Create(Form1.CachePath, settings);
  37. doc.Load(reader);
  38. //先得到根节点
  39. XmlNode rootNode = doc.SelectSingleNode("CacheInf");
  40. //再由根节点去找制定的节点
  41. XmlNodeList nodeList = rootNode.ChildNodes;
  42. foreach (XmlNode node in nodeList)
  43. {
  44. //找到了这个节点名字
  45. if (node.Name == ParamName)
  46. {
  47. //返回节点的内容
  48. switch (((XmlElement)node).GetAttribute("Type"))
  49. {
  50. case "System.String":
  51. returnData = node.InnerText;
  52. break;
  53. case "System.Int32":
  54. returnData = int.Parse(node.InnerText);
  55. break;
  56. case "System.Boolean":
  57. returnData = node.InnerText == "True" ? true : false;
  58. break;
  59. default:
  60. break;
  61. }
  62. break;
  63. }
  64. }
  65. //关闭reader
  66. reader.Close();
  67. if (returnData == null)
  68. return "";
  69. else
  70. return returnData;
  71. }
  72. catch (Exception)
  73. {
  74. return "";
  75. }
  76. }
  77. public static void SetCacheData(string ParamName, object Value)
  78. {
  79. try
  80. {
  81. //根据地址读取xml文件
  82. XmlDocument doc = new XmlDocument();
  83. XmlReaderSettings settings = new XmlReaderSettings();
  84. //忽略文档里面的注释
  85. settings.IgnoreComments = true;
  86. XmlReader reader = XmlReader.Create(Form1.CachePath, settings);
  87. doc.Load(reader);
  88. //先得到根节点
  89. XmlNode rootNode = doc.SelectSingleNode("CacheInf");
  90. //再由根节点去找制定的节点
  91. XmlNodeList nodeList = rootNode.ChildNodes;
  92. bool flag = false;
  93. foreach (XmlNode node in nodeList)
  94. {
  95. //找到了这个节点名字
  96. if (node.Name == ParamName)
  97. {
  98. //就直接赋值
  99. node.InnerText = Value.ToString();
  100. flag = true;
  101. }
  102. }
  103. //如果没有该节点,就创建节点保存结果
  104. if (!flag)
  105. {
  106. //创建节点
  107. XmlElement newNode = doc.CreateElement(ParamName);
  108. XmlAttribute attr = doc.CreateAttribute("Type");
  109. attr.InnerText = Value.GetType().ToString();
  110. newNode.InnerText = Value.ToString();
  111. newNode.SetAttributeNode(attr);
  112. //讲新建的节点挂到根节点上
  113. rootNode.AppendChild(newNode);
  114. }
  115. //关闭Reader
  116. reader.Close();
  117. doc.Save(Form1.CachePath);
  118. }
  119. catch (Exception ex)
  120. {
  121. Console.WriteLine(ex.Message);
  122. }
  123. }
  124. }
  125. }