Helper_XMLHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Xml;
  16. namespace iNethinkCMS.Helper
  17. {
  18. public class XMLHelper
  19. {
  20. public XMLHelper()
  21. {
  22. }
  23. #region XML文档节点查询和读取
  24. /// <summary>
  25. /// 选择匹配XPath表达式的第一个节点XmlNode.
  26. /// </summary>
  27. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  28. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
  29. /// <returns>返回XmlNode</returns>
  30. public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)
  31. {
  32. XmlDocument xmlDoc = new XmlDocument();
  33. try
  34. {
  35. xmlDoc.Load(xmlFileName); //加载XML文档
  36. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  37. return xmlNode;
  38. }
  39. catch (Exception ex)
  40. {
  41. return null;
  42. throw ex; //这里可以定义你自己的异常处理
  43. }
  44. }
  45. /// <summary>
  46. /// 选择匹配XPath表达式的节点列表XmlNodeList.
  47. /// </summary>
  48. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  49. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
  50. /// <returns>返回XmlNodeList</returns>
  51. public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
  52. {
  53. XmlDocument xmlDoc = new XmlDocument();
  54. try
  55. {
  56. xmlDoc.Load(xmlFileName); //加载XML文档
  57. XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);
  58. return xmlNodeList;
  59. }
  60. catch (Exception ex)
  61. {
  62. return null;
  63. throw ex; //这里可以定义你自己的异常处理
  64. }
  65. }
  66. /// <summary>
  67. /// 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute.
  68. /// </summary>
  69. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  70. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  71. /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
  72. /// <returns>返回xmlAttributeName</returns>
  73. public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)
  74. {
  75. string content = string.Empty;
  76. XmlDocument xmlDoc = new XmlDocument();
  77. XmlAttribute xmlAttribute = null;
  78. try
  79. {
  80. xmlDoc.Load(xmlFileName); //加载XML文档
  81. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  82. if (xmlNode != null)
  83. {
  84. if (xmlNode.Attributes.Count > 0)
  85. {
  86. xmlAttribute = xmlNode.Attributes[xmlAttributeName];
  87. }
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. throw ex; //这里可以定义你自己的异常处理
  93. }
  94. return xmlAttribute;
  95. }
  96. #endregion
  97. #region XML文档创建和节点或属性的添加、修改
  98. /// <summary>
  99. /// 创建一个XML文档
  100. /// </summary>
  101. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  102. /// <param name="rootNodeName">XML文档根节点名称(须指定一个根节点名称)</param>
  103. /// <param name="version">XML文档版本号(必须为:"1.0")</param>
  104. /// <param name="encoding">XML文档编码方式</param>
  105. /// <param name="standalone">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</param>
  106. /// <returns>成功返回true,失败返回false</returns>
  107. public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone)
  108. {
  109. bool isSuccess = false;
  110. try
  111. {
  112. XmlDocument xmlDoc = new XmlDocument();
  113. XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);
  114. XmlNode root = xmlDoc.CreateElement(rootNodeName);
  115. xmlDoc.AppendChild(xmlDeclaration);
  116. xmlDoc.AppendChild(root);
  117. xmlDoc.Save(xmlFileName);
  118. isSuccess = true;
  119. }
  120. catch (Exception ex)
  121. {
  122. throw ex; //这里可以定义你自己的异常处理
  123. }
  124. return isSuccess;
  125. }
  126. /// <summary>
  127. /// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点
  128. /// </summary>
  129. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  130. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  131. /// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
  132. /// <param name="innerText">节点文本值</param>
  133. /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
  134. /// <param name="value">属性值</param>
  135. /// <returns>成功返回true,失败返回false</returns>
  136. public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string xmlAttributeName, string value)
  137. {
  138. bool isSuccess = false;
  139. XmlDocument xmlDoc = new XmlDocument();
  140. try
  141. {
  142. xmlDoc.Load(xmlFileName); //加载XML文档
  143. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  144. if (xmlNode != null)
  145. {
  146. //存不存在此节点都创建
  147. XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
  148. subElement.InnerXml = innerText;
  149. //如果属性和值参数都不为空则在此新节点上新增属性
  150. if (!string.IsNullOrEmpty(xmlAttributeName) && !string.IsNullOrEmpty(value))
  151. {
  152. XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
  153. xmlAttribute.Value = value;
  154. subElement.Attributes.Append(xmlAttribute);
  155. }
  156. xmlNode.AppendChild(subElement);
  157. }
  158. xmlDoc.Save(xmlFileName); //保存到XML文档
  159. isSuccess = true;
  160. }
  161. catch (Exception ex)
  162. {
  163. throw ex; //这里可以定义你自己的异常处理
  164. }
  165. return isSuccess;
  166. }
  167. /// <summary>
  168. /// 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建)
  169. /// </summary>
  170. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  171. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  172. /// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
  173. /// <param name="innerText">节点文本值</param>
  174. /// <returns>成功返回true,失败返回false</returns>
  175. public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText)
  176. {
  177. bool isSuccess = false;
  178. bool isExistsNode = false;//标识节点是否存在
  179. XmlDocument xmlDoc = new XmlDocument();
  180. try
  181. {
  182. xmlDoc.Load(xmlFileName); //加载XML文档
  183. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  184. if (xmlNode != null)
  185. {
  186. //遍历xpath节点下的所有子节点
  187. foreach (XmlNode node in xmlNode.ChildNodes)
  188. {
  189. if (node.Name.ToLower() == xmlNodeName.ToLower())
  190. {
  191. //存在此节点则更新
  192. node.InnerXml = innerText;
  193. isExistsNode = true;
  194. break;
  195. }
  196. }
  197. if (!isExistsNode)
  198. {
  199. //不存在此节点则创建
  200. XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
  201. subElement.InnerXml = innerText;
  202. xmlNode.AppendChild(subElement);
  203. }
  204. }
  205. xmlDoc.Save(xmlFileName); //保存到XML文档
  206. isSuccess = true;
  207. }
  208. catch (Exception ex)
  209. {
  210. throw ex; //这里可以定义你自己的异常处理
  211. }
  212. return isSuccess;
  213. }
  214. /// <summary>
  215. /// 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建)
  216. /// </summary>
  217. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  218. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  219. /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
  220. /// <param name="value">属性值</param>
  221. /// <returns>成功返回true,失败返回false</returns>
  222. public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value)
  223. {
  224. bool isSuccess = false;
  225. bool isExistsAttribute = false;//标识属性是否存在
  226. XmlDocument xmlDoc = new XmlDocument();
  227. try
  228. {
  229. xmlDoc.Load(xmlFileName); //加载XML文档
  230. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  231. if (xmlNode != null)
  232. {
  233. //遍历xpath节点中的所有属性
  234. foreach (XmlAttribute attribute in xmlNode.Attributes)
  235. {
  236. if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
  237. {
  238. //节点中存在此属性则更新
  239. attribute.Value = value;
  240. isExistsAttribute = true;
  241. break;
  242. }
  243. }
  244. if (!isExistsAttribute)
  245. {
  246. //节点中不存在此属性则创建
  247. XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
  248. xmlAttribute.Value = value;
  249. xmlNode.Attributes.Append(xmlAttribute);
  250. }
  251. }
  252. xmlDoc.Save(xmlFileName); //保存到XML文档
  253. isSuccess = true;
  254. }
  255. catch (Exception ex)
  256. {
  257. throw ex; //这里可以定义你自己的异常处理
  258. }
  259. return isSuccess;
  260. }
  261. #endregion
  262. #region XML文档节点或属性的删除
  263. /// <summary>
  264. /// 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除)
  265. /// </summary>
  266. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  267. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  268. /// <returns>成功返回true,失败返回false</returns>
  269. public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
  270. {
  271. bool isSuccess = false;
  272. XmlDocument xmlDoc = new XmlDocument();
  273. try
  274. {
  275. xmlDoc.Load(xmlFileName); //加载XML文档
  276. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  277. if (xmlNode != null)
  278. {
  279. //删除节点
  280. xmlNode.ParentNode.RemoveChild(xmlNode);
  281. }
  282. xmlDoc.Save(xmlFileName); //保存到XML文档
  283. isSuccess = true;
  284. }
  285. catch (Exception ex)
  286. {
  287. throw ex; //这里可以定义你自己的异常处理
  288. }
  289. return isSuccess;
  290. }
  291. /// <summary>
  292. /// 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性
  293. /// </summary>
  294. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  295. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  296. /// <param name="xmlAttributeName">要删除的xmlAttributeName的属性名称</param>
  297. /// <returns>成功返回true,失败返回false</returns>
  298. public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
  299. {
  300. bool isSuccess = false;
  301. bool isExistsAttribute = false;
  302. XmlDocument xmlDoc = new XmlDocument();
  303. try
  304. {
  305. xmlDoc.Load(xmlFileName); //加载XML文档
  306. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  307. XmlAttribute xmlAttribute = null;
  308. if (xmlNode != null)
  309. {
  310. //遍历xpath节点中的所有属性
  311. foreach (XmlAttribute attribute in xmlNode.Attributes)
  312. {
  313. if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
  314. {
  315. //节点中存在此属性
  316. xmlAttribute = attribute;
  317. isExistsAttribute = true;
  318. break;
  319. }
  320. }
  321. if (isExistsAttribute)
  322. {
  323. //删除节点中的属性
  324. xmlNode.Attributes.Remove(xmlAttribute);
  325. }
  326. }
  327. xmlDoc.Save(xmlFileName); //保存到XML文档
  328. isSuccess = true;
  329. }
  330. catch (Exception ex)
  331. {
  332. throw ex; //这里可以定义你自己的异常处理
  333. }
  334. return isSuccess;
  335. }
  336. /// <summary>
  337. /// 删除匹配XPath表达式的第一个节点中的所有属性
  338. /// </summary>
  339. /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
  340. /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
  341. /// <returns>成功返回true,失败返回false</returns>
  342. public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
  343. {
  344. bool isSuccess = false;
  345. XmlDocument xmlDoc = new XmlDocument();
  346. try
  347. {
  348. xmlDoc.Load(xmlFileName); //加载XML文档
  349. XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
  350. if (xmlNode != null)
  351. {
  352. //遍历xpath节点中的所有属性
  353. xmlNode.Attributes.RemoveAll();
  354. }
  355. xmlDoc.Save(xmlFileName); //保存到XML文档
  356. isSuccess = true;
  357. }
  358. catch (Exception ex)
  359. {
  360. throw ex; //这里可以定义你自己的异常处理
  361. }
  362. return isSuccess;
  363. }
  364. #endregion
  365. }
  366. }