XMLOperationClass.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //using COTSXmlFileDll;
  2. using System;
  3. using System.Xml;
  4. namespace OTSSysMgrApp
  5. {
  6. public class XMLOperationClass
  7. {
  8. #region 全部变量声明
  9. //获取XML 路径
  10. static string xmlFilePath = System.Configuration.ConfigurationManager.ConnectionStrings["XMLFilePath"].ConnectionString;
  11. #endregion
  12. public XMLOperationClass()
  13. { }
  14. #region 操作XML方法
  15. /// <summary>
  16. /// 判断Xml节点是否存在
  17. /// </summary>
  18. public static bool ExistsXmlInfo(string Name)
  19. {
  20. XmlNode SelectNode = null;
  21. XmlDocument doc = new XmlDocument();
  22. doc.Load(xmlFilePath); //加载Xml文件
  23. XmlElement root = doc.DocumentElement; //获取根节点
  24. XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合
  25. foreach (XmlNode node in mainNodes)
  26. {
  27. //获取Name属性值
  28. string name = ((XmlElement)node).GetAttribute("Name");
  29. //获取Age子XmlElement集合
  30. string value = ((XmlElement)node).GetAttribute("Value");
  31. if (name.Equals(Name))
  32. {
  33. SelectNode = node;
  34. if (SelectNode != null)
  35. {
  36. return true;
  37. }
  38. }
  39. }
  40. return false;
  41. }
  42. /// <summary>
  43. /// 添加Xml节点
  44. /// </summary>
  45. /// <param name="name">标识</param>
  46. /// <param name="value">参数值</param>
  47. public static void AddXmlInfo(string Name, string Value)
  48. {
  49. try
  50. {
  51. XmlDocument doc = new XmlDocument();
  52. doc.Load(xmlFilePath);
  53. XmlElement root = doc.DocumentElement;
  54. //根节点的添加独立子节点
  55. XmlElement systemData = doc.CreateElement("parameter");
  56. //设置参数
  57. systemData.SetAttribute("Name", Name);
  58. systemData.SetAttribute("Value", Value);
  59. //添加至父节点
  60. root.AppendChild(systemData);
  61. //保存
  62. doc.Save(xmlFilePath);
  63. }
  64. catch (Exception)
  65. {
  66. //记录日志信息
  67. }
  68. }
  69. /// <summary>
  70. /// 编辑XML
  71. /// </summary>
  72. public static void EditXmlInfo(string Name, string Value)
  73. {
  74. XmlDocument doc = new XmlDocument();
  75. doc.Load(xmlFilePath); //加载Xml文件
  76. XmlElement root = doc.DocumentElement; //获取根节点
  77. XmlNodeList personNodes = root.GetElementsByTagName("parameter"); //获取Person子节点集合
  78. foreach (XmlNode node in personNodes)
  79. {
  80. XmlElement ele = (XmlElement)node;
  81. if (ele.GetAttribute("Name") == Name)
  82. {
  83. ele.Attributes["Value"].Value = Value;
  84. }
  85. }
  86. doc.Save(xmlFilePath);
  87. }
  88. /// <summary>
  89. /// 修改xml
  90. /// </summary>
  91. /// <param name="FilePath">文件地址</param>
  92. /// <param name="RegName">节点名称</param>
  93. /// <param name="Name">节点属性名称</param>
  94. /// <param name="Value">节点属性值</param>
  95. public static void EditXmlInfoWidthPath(string FilePath,string RegName,string Name, string Value)
  96. {
  97. XmlDocument xmlDoc = new XmlDocument();
  98. xmlDoc.Load(FilePath); //加载Xml文件
  99. XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member");
  100. foreach (XmlNode node in nodeList)
  101. {
  102. XmlElement ele = (XmlElement)node;
  103. if (ele.GetAttribute("RegName") == RegName)
  104. {
  105. ele.SetAttribute(Name, Value);
  106. }
  107. }
  108. xmlDoc.Save(FilePath);
  109. }
  110. #endregion
  111. #region 获取XML节点数据
  112. /// <summary>
  113. /// 获取节点数据
  114. /// </summary>
  115. /// <param name="FilePath">文件地址</param>
  116. /// <param name="RegName">节点名称</param>
  117. /// <param name="Name">节点属性</param>
  118. /// <returns></returns>
  119. public static string GetXMLReg(string FilePath, string RegName, string Name)
  120. {
  121. XmlDocument xmlDoc = new XmlDocument();
  122. xmlDoc.Load(FilePath); //加载Xml文件
  123. XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member");
  124. string str = "";
  125. foreach (XmlNode node in nodeList)
  126. {
  127. XmlElement ele = (XmlElement)node;
  128. if (ele.GetAttribute("RegName") == RegName)
  129. {
  130. str = ele.GetAttribute(Name);
  131. break;
  132. }
  133. }
  134. return str;
  135. }
  136. #endregion
  137. #region 获取XML节点参数
  138. /// <summary>
  139. /// 获取XML节点参数
  140. /// </summary>
  141. /// <param name="Name">节点参数名称</param>
  142. /// <returns>节点参数</returns>
  143. public static XmlNode GetXMLInformation(string Name)
  144. {
  145. try
  146. {
  147. XmlNode SelectNode = null;
  148. XmlDocument doc = new XmlDocument();
  149. doc.Load(xmlFilePath); //加载Xml文件
  150. XmlElement root = doc.DocumentElement; //获取根节点
  151. XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合
  152. foreach (XmlNode node in mainNodes)
  153. {
  154. //获取Name属性值
  155. string name = ((XmlElement)node).GetAttribute("Name");
  156. //获取Age子XmlElement集合
  157. string value = ((XmlElement)node).GetAttribute("Value");
  158. if (name.Equals(Name))
  159. {
  160. SelectNode = node;
  161. }
  162. }
  163. return SelectNode;
  164. }
  165. catch (Exception)
  166. {
  167. return null;
  168. }
  169. }
  170. #endregion
  171. #region 获取XML节点参数
  172. /// <summary>
  173. /// 获取XML节点参数
  174. /// </summary>
  175. /// <param name="Name">节点参数名称</param>
  176. /// <returns>节点参数</returns>
  177. public static string GetXMLInformations(string Name)
  178. {
  179. try
  180. {
  181. string value = string.Empty;
  182. XmlDocument doc = new XmlDocument();
  183. doc.Load(xmlFilePath); //加载Xml文件
  184. XmlElement root = doc.DocumentElement; //获取根节点
  185. XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合
  186. foreach (XmlNode node in mainNodes)
  187. {
  188. //获取Name属性值
  189. string name = ((XmlElement)node).GetAttribute("Name");
  190. if (name.Equals(Name))
  191. {
  192. value = ((XmlElement)node).GetAttribute("Value");
  193. }
  194. }
  195. return value;
  196. }
  197. catch (Exception)
  198. {
  199. return "";
  200. }
  201. }
  202. #endregion
  203. #region 获取窗体中所有参数
  204. /// <summary>
  205. /// 获取窗体中控件的所有参数
  206. /// </summary>
  207. public static XmlNode GetXMLInformationValue(string selNodeName)
  208. {
  209. try
  210. {
  211. XmlNode selectNode = null;
  212. XmlDocument doc = new XmlDocument();
  213. //加载Xml文件
  214. doc.Load(xmlFilePath);
  215. //获取根节点
  216. XmlElement root = doc.DocumentElement;
  217. //获取子节点集合
  218. XmlNodeList mainNodes = root.GetElementsByTagName("parameter");
  219. foreach (XmlNode node in mainNodes)
  220. {
  221. string NodeName = ((XmlElement)node).GetAttribute("Name");
  222. string NodeValue = ((XmlElement)node).GetAttribute("Value");
  223. if (NodeName == selNodeName)
  224. {
  225. //获取属性名称与属性值
  226. selectNode = node;
  227. break;
  228. }
  229. }
  230. return selectNode;
  231. }
  232. catch (Exception)
  233. {
  234. return null;
  235. }
  236. }
  237. #endregion
  238. #region 写入COTSXMLFileDllExportForCShape
  239. public static bool WriteXMLFile(string FileName,string SEMName, string EDSName)
  240. {
  241. try
  242. {
  243. EditXmlInfoWidthPath(FileName, "SemControllerName", "drive", SEMName);
  244. EditXmlInfoWidthPath(FileName, "EDSName", "drive", EDSName);
  245. return true;
  246. }
  247. catch (Exception)
  248. {
  249. return false;
  250. }
  251. }
  252. public static bool WriteXMLFileVersion(string FileName, string selectVersion)
  253. {
  254. try
  255. {
  256. EditXmlInfoWidthPath(FileName, "BrukerDllVersion", "Version", selectVersion);
  257. return true;
  258. }
  259. catch (Exception)
  260. {
  261. return false;
  262. }
  263. }
  264. #endregion
  265. #region 读取COTSXMLFileDllExportForCShape
  266. public static bool ReadXMLFile(string FileName,ref string SEMName, ref string EDSName)
  267. {
  268. try
  269. {
  270. SEMName = GetXMLReg(FileName, "SemControllerName", "drive");
  271. EDSName = GetXMLReg(FileName, "EDSName", "drive");
  272. return true;
  273. }
  274. catch (Exception)
  275. {
  276. return false;
  277. }
  278. }
  279. public static bool ReadXMLFileVersion(string FileName, ref string selectVersion)
  280. {
  281. try
  282. {
  283. selectVersion = GetXMLReg(FileName, "BrukerDllVersion", "Version");
  284. return true;
  285. }
  286. catch (Exception)
  287. {
  288. return false;
  289. }
  290. }
  291. #endregion
  292. }
  293. }