XMLOperationClass.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 bool EditXmlInfoWidthPath(string FilePath,string RegName,string Name, string Value)
  96. {
  97. bool isSuccess = false;
  98. XmlDocument xmlDoc = new XmlDocument();
  99. xmlDoc.Load(FilePath); //加载Xml文件
  100. XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member");
  101. foreach (XmlNode node in nodeList)
  102. {
  103. XmlElement ele = (XmlElement)node;
  104. if (ele.GetAttribute("RegName") == RegName)
  105. {
  106. ele.SetAttribute(Name, Value);
  107. isSuccess = true;
  108. break;
  109. }
  110. }
  111. if (isSuccess)
  112. {
  113. xmlDoc.Save(FilePath);
  114. }
  115. return isSuccess;
  116. }
  117. #endregion
  118. #region 获取XML节点数据
  119. /// <summary>
  120. /// 获取节点数据
  121. /// </summary>
  122. /// <param name="FilePath">文件地址</param>
  123. /// <param name="RegName">节点名称</param>
  124. /// <param name="Name">节点属性</param>
  125. /// <returns></returns>
  126. public static string GetXMLReg(string FilePath, string RegName, string Name)
  127. {
  128. XmlDocument xmlDoc = new XmlDocument();
  129. xmlDoc.Load(FilePath); //加载Xml文件
  130. XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member");
  131. string str = "";
  132. foreach (XmlNode node in nodeList)
  133. {
  134. XmlElement ele = (XmlElement)node;
  135. if (ele.GetAttribute("RegName") == RegName)
  136. {
  137. str = ele.GetAttribute(Name);
  138. break;
  139. }
  140. }
  141. return str;
  142. }
  143. #endregion
  144. #region 获取XML节点参数
  145. /// <summary>
  146. /// 获取XML节点参数
  147. /// </summary>
  148. /// <param name="Name">节点参数名称</param>
  149. /// <returns>节点参数</returns>
  150. public static XmlNode GetXMLInformation(string Name)
  151. {
  152. try
  153. {
  154. XmlNode SelectNode = null;
  155. XmlDocument doc = new XmlDocument();
  156. doc.Load(xmlFilePath); //加载Xml文件
  157. XmlElement root = doc.DocumentElement; //获取根节点
  158. XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合
  159. foreach (XmlNode node in mainNodes)
  160. {
  161. //获取Name属性值
  162. string name = ((XmlElement)node).GetAttribute("Name");
  163. //获取Age子XmlElement集合
  164. string value = ((XmlElement)node).GetAttribute("Value");
  165. if (name.Equals(Name))
  166. {
  167. SelectNode = node;
  168. }
  169. }
  170. return SelectNode;
  171. }
  172. catch (Exception)
  173. {
  174. return null;
  175. }
  176. }
  177. #endregion
  178. #region 获取XML节点参数
  179. /// <summary>
  180. /// 获取XML节点参数
  181. /// </summary>
  182. /// <param name="Name">节点参数名称</param>
  183. /// <returns>节点参数</returns>
  184. public static string GetXMLInformations(string Name)
  185. {
  186. try
  187. {
  188. string value = string.Empty;
  189. XmlDocument doc = new XmlDocument();
  190. doc.Load(xmlFilePath); //加载Xml文件
  191. XmlElement root = doc.DocumentElement; //获取根节点
  192. XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合
  193. foreach (XmlNode node in mainNodes)
  194. {
  195. //获取Name属性值
  196. string name = ((XmlElement)node).GetAttribute("Name");
  197. if (name.Equals(Name))
  198. {
  199. value = ((XmlElement)node).GetAttribute("Value");
  200. }
  201. }
  202. return value;
  203. }
  204. catch (Exception)
  205. {
  206. return "";
  207. }
  208. }
  209. #endregion
  210. #region 获取窗体中所有参数
  211. /// <summary>
  212. /// 获取窗体中控件的所有参数
  213. /// </summary>
  214. public static XmlNode GetXMLInformationValue(string selNodeName)
  215. {
  216. try
  217. {
  218. XmlNode selectNode = null;
  219. XmlDocument doc = new XmlDocument();
  220. //加载Xml文件
  221. doc.Load(xmlFilePath);
  222. //获取根节点
  223. XmlElement root = doc.DocumentElement;
  224. //获取子节点集合
  225. XmlNodeList mainNodes = root.GetElementsByTagName("parameter");
  226. foreach (XmlNode node in mainNodes)
  227. {
  228. string NodeName = ((XmlElement)node).GetAttribute("Name");
  229. string NodeValue = ((XmlElement)node).GetAttribute("Value");
  230. if (NodeName == selNodeName)
  231. {
  232. //获取属性名称与属性值
  233. selectNode = node;
  234. break;
  235. }
  236. }
  237. return selectNode;
  238. }
  239. catch (Exception)
  240. {
  241. return null;
  242. }
  243. }
  244. #endregion
  245. #region 写入COTSXMLFileDllExportForCShape
  246. public static bool WriteXMLFile(string FileName,string SEMName, string EDSName)
  247. {
  248. try
  249. {
  250. bool isSuccessSem = false;
  251. bool isSuccessEds = false;
  252. isSuccessSem = EditXmlInfoWidthPath(FileName, "SemControllerName", "Value", SEMName);
  253. isSuccessEds = EditXmlInfoWidthPath(FileName, "EDSName", "Value", EDSName);
  254. return isSuccessSem && isSuccessEds;
  255. }
  256. catch (Exception)
  257. {
  258. return false;
  259. }
  260. }
  261. #endregion
  262. #region 读取COTSXMLFileDllExportForCShape
  263. public static bool ReadXMLFile(string FileName,ref string SEMName, ref string EDSName)
  264. {
  265. try
  266. {
  267. SEMName = GetXMLReg(FileName, "SemControllerName", "Value");
  268. EDSName = GetXMLReg(FileName, "EDSName", "Value");
  269. return true;
  270. }
  271. catch (Exception)
  272. {
  273. return false;
  274. }
  275. }
  276. #endregion
  277. public static bool WriteSysType(string xmlpath, string AttributeName, string Value)
  278. {
  279. try
  280. {
  281. XmlDocument xmlDocument = new XmlDocument();
  282. xmlDocument.Load(xmlpath);
  283. XmlNode node = xmlDocument.SelectSingleNode("XMLData");
  284. XmlElement xe = (XmlElement)node;
  285. xe.SetAttribute(AttributeName, Value);
  286. xmlDocument.Save(xmlpath);
  287. }
  288. catch /*(Exception ex)*/
  289. {
  290. //MessageBox.Show(ex.ToString());
  291. return false;
  292. }
  293. return true;
  294. }
  295. public static bool ReadSysType(string xmlpath, string AttributeName,ref string Value)
  296. {
  297. try
  298. {
  299. XmlDocument xmlDocument = new XmlDocument();
  300. xmlDocument.Load(xmlpath);
  301. XmlNode node = xmlDocument.SelectSingleNode("XMLData");
  302. XmlElement xe = (XmlElement)node;
  303. Value=xe.GetAttribute(AttributeName);
  304. }
  305. catch /*(Exception ex)*/
  306. {
  307. //MessageBox.Show(ex.ToString());
  308. return false;
  309. }
  310. return true;
  311. }
  312. }
  313. }