MyXml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Xml;
  5. namespace Core.StlMes.Client.LgCommon
  6. {
  7. public class MyXml
  8. {
  9. public static XmlDocument GetXmlDoc(string FileName)
  10. {
  11. try
  12. {
  13. XmlDocument xmldoc = new XmlDocument();
  14. if (!File.Exists(FileName))
  15. {
  16. CreateXmlDeclaration(xmldoc, "1.0", "utf-8");
  17. }
  18. else
  19. {
  20. xmldoc.Load(FileName);
  21. }
  22. return xmldoc;
  23. }
  24. catch (Exception ex)
  25. {
  26. MessageBox.Show("读取XML失败!\n" + "文件名:" + FileName + "\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  27. return null;
  28. }
  29. }
  30. public static void CreateXmlDeclaration(XmlDocument xmldoc, string Version, string encoding)
  31. {
  32. try
  33. {
  34. if (xmldoc != null)
  35. {
  36. XmlNodeList nodelist = xmldoc.ChildNodes;
  37. if (nodelist != null)
  38. {
  39. foreach (XmlNode xn in nodelist)
  40. {
  41. if (xn.NodeType == XmlNodeType.XmlDeclaration)
  42. {
  43. return;
  44. }
  45. }
  46. }
  47. XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
  48. xmldoc.AppendChild(xmlnode);
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53. MessageBox.Show("XML创建声明失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  54. }
  55. }
  56. public static XmlNode GetXmlRoot(XmlDocument xmldoc)
  57. {
  58. try
  59. {
  60. if (xmldoc != null)
  61. {
  62. return xmldoc.DocumentElement;
  63. }
  64. else
  65. {
  66. MessageBox.Show("XmlDocument为空对象!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  67. }
  68. return null;
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageBox.Show("根节点获取失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  73. return null;
  74. }
  75. }
  76. public static XmlNode CreateXmlRoot(XmlDocument xmldoc, string rootName)
  77. {
  78. XmlNode root = GetXmlRoot(xmldoc);
  79. if (root != null && root.Name == rootName)
  80. {
  81. return root;
  82. }
  83. try
  84. {
  85. if (xmldoc != null)
  86. {
  87. if (root != null && root.Name != rootName)
  88. {
  89. xmldoc.RemoveChild(root);
  90. }
  91. root = xmldoc.CreateNode(XmlNodeType.Element, rootName, null);
  92. xmldoc.AppendChild(root);
  93. return root;
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show("根节点设置失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  99. }
  100. return null;
  101. }
  102. public static XmlNode CreateXmlNode(XmlDocument xmldoc, XmlNode parent, string NodeName, string xmlnodeAttribute, string xmlnodeAttributeValue)
  103. {
  104. try
  105. {
  106. if (xmldoc != null && parent != null)
  107. {
  108. XmlNodeList nodelist = parent.ChildNodes;
  109. if (nodelist != null)
  110. {
  111. foreach (XmlNode xn in nodelist)
  112. {
  113. if (xn.Name == NodeName)
  114. {
  115. foreach (XmlAttribute attribute in xn.Attributes)
  116. {
  117. if (attribute.Name == xmlnodeAttribute &&
  118. attribute.Value == xmlnodeAttributeValue)
  119. {
  120. return xn;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. XmlNode xmlnode = xmldoc.CreateElement(NodeName);
  127. xmlnode.Attributes.Append(xmldoc.CreateAttribute(xmlnodeAttribute)).Value = xmlnodeAttributeValue;
  128. parent.AppendChild(xmlnode);
  129. return xmlnode;
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. MessageBox.Show("创建XML节点【" + NodeName + "】失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  135. }
  136. return null;
  137. }
  138. public static XmlNode AddXmlNode(XmlDocument xmldoc, XmlNode parent, string NodeName, bool canRepeated)
  139. {
  140. try
  141. {
  142. if (xmldoc != null && parent != null)
  143. {
  144. if (!canRepeated)
  145. {
  146. XmlNodeList nodelist = parent.ChildNodes;
  147. if (nodelist != null)
  148. {
  149. foreach (XmlNode xn in nodelist)
  150. {
  151. if (xn.Name == NodeName)
  152. {
  153. return xn;
  154. }
  155. }
  156. }
  157. }
  158. XmlNode xmlnode = xmldoc.CreateElement(NodeName);
  159. parent.AppendChild(xmlnode);
  160. return xmlnode;
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. MessageBox.Show("新增XML节点【" + NodeName + "】失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  166. }
  167. return null;
  168. }
  169. public static void AddXmlNodeAttribute(XmlDocument xmldoc, XmlNode xmlnode, string xmlnodeAttribute, string xmlnodeAttributeValue, bool canRepeated)
  170. {
  171. try
  172. {
  173. if (xmldoc != null && xmlnode != null)
  174. {
  175. if (!canRepeated)
  176. {
  177. foreach (XmlAttribute attribute in xmlnode.Attributes)
  178. {
  179. if (attribute.Name == xmlnodeAttribute)
  180. {
  181. return;
  182. }
  183. }
  184. }
  185. XmlAttribute node = xmldoc.CreateAttribute(xmlnodeAttribute);
  186. node.Value = xmlnodeAttributeValue;
  187. xmlnode.Attributes.Append(node);
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. MessageBox.Show("节点新增属性【" + xmlnodeAttribute + "】失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  193. }
  194. }
  195. public static void SetXmlNodeAttributeValue(XmlNode xmlnode, string xmlnodeAttribute, string xmlnodeAttributeValue)
  196. {
  197. try
  198. {
  199. if (xmlnode != null)
  200. {
  201. foreach (XmlAttribute attribute in xmlnode.Attributes)
  202. {
  203. if (attribute.Name == xmlnodeAttribute)
  204. {
  205. attribute.Value = xmlnodeAttributeValue;
  206. }
  207. }
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. MessageBox.Show("设置节点属性 " + xmlnodeAttribute + " = " + xmlnodeAttributeValue + " 失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  213. }
  214. }
  215. public static string GetXmlNodeAttributeValue(XmlDocument xmldoc, string xpath, string NodeName, string xmlnodeAttributeWhen, string xmlnodeAttributeValueWhen, string xmlnodeAttributeThen)
  216. {
  217. try
  218. {
  219. if (xmldoc != null)
  220. {
  221. XmlNodeList nodelist = xmldoc.SelectNodes(xpath);
  222. if (nodelist != null)
  223. {
  224. bool OK = false;
  225. foreach (XmlNode xmlnode in nodelist)
  226. {
  227. if (xmlnode.Name == NodeName)
  228. {
  229. OK = false;
  230. foreach (XmlAttribute attribute in xmlnode.Attributes)
  231. {
  232. if (attribute.Name == xmlnodeAttributeWhen && attribute.Value == xmlnodeAttributeValueWhen)
  233. {
  234. OK = true;
  235. break;
  236. }
  237. }
  238. if (OK)
  239. {
  240. foreach (XmlAttribute attribute in xmlnode.Attributes)
  241. {
  242. if (attribute.Name == xmlnodeAttributeThen)
  243. {
  244. return attribute.Value;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. MessageBox.Show("获取节点属性 " + xmlnodeAttributeThen + " 值失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  256. }
  257. return "";
  258. }
  259. public static bool SetXmlNodeAttributeValue(XmlDocument xmldoc, string xpath, string NodeName, string xmlnodeAttributeWhen, string xmlnodeAttributeValueWhen, string xmlnodeAttributeThen, string xmlnodeAttributeValueThen)
  260. {
  261. try
  262. {
  263. if (xmldoc != null)
  264. {
  265. XmlNodeList nodelist = xmldoc.SelectNodes(xpath);
  266. if (nodelist != null)
  267. {
  268. int iDone = 0;
  269. bool OK = false;
  270. foreach (XmlNode xmlnode in nodelist)
  271. {
  272. if (xmlnode.Name == NodeName)
  273. {
  274. OK = false;
  275. foreach (XmlAttribute attribute in xmlnode.Attributes)
  276. {
  277. if (attribute.Name == xmlnodeAttributeWhen && attribute.Value == xmlnodeAttributeValueWhen)
  278. {
  279. OK = true;
  280. break;
  281. }
  282. }
  283. if (OK)
  284. {
  285. foreach (XmlAttribute attribute in xmlnode.Attributes)
  286. {
  287. if (attribute.Name == xmlnodeAttributeThen)
  288. {
  289. attribute.Value = xmlnodeAttributeValueThen;
  290. iDone++;
  291. }
  292. }
  293. }
  294. }
  295. }
  296. if (iDone > 0)
  297. {
  298. return true;
  299. }
  300. else
  301. {
  302. MessageBox.Show("配置文件不存在属性 " + xmlnodeAttributeWhen + " = " + xmlnodeAttributeValueWhen + " !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  303. return false;
  304. }
  305. }
  306. }
  307. }
  308. catch (Exception ex)
  309. {
  310. MessageBox.Show("设置节点属性 " + xmlnodeAttributeThen + " = " + xmlnodeAttributeValueThen + " 失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  311. }
  312. return false;
  313. }
  314. }
  315. }