市长热线演示版

XmlHelper.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. namespace HySoft.Common
  6. {
  7. public class XmlHelper
  8. {
  9. #region 增、删、改操作==============================================
  10. /// <summary>
  11. /// 追加节点
  12. /// </summary>
  13. /// <param name="filePath">XML文档绝对路径</param>
  14. /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
  15. /// <param name="xmlNode">XmlNode节点</param>
  16. /// <returns></returns>
  17. public static bool AppendChild(string filePath, string xPath, XmlNode xmlNode)
  18. {
  19. try
  20. {
  21. XmlDocument doc = new XmlDocument();
  22. doc.Load(filePath);
  23. XmlNode xn = doc.SelectSingleNode(xPath);
  24. XmlNode n = doc.ImportNode(xmlNode, true);
  25. xn.AppendChild(n);
  26. doc.Save(filePath);
  27. return true;
  28. }
  29. catch
  30. {
  31. return false;
  32. }
  33. }
  34. /// <summary>
  35. /// 从XML文档中读取节点追加到另一个XML文档中
  36. /// </summary>
  37. /// <param name="filePath">需要读取的XML文档绝对路径</param>
  38. /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
  39. /// <param name="toFilePath">被追加节点的XML文档绝对路径</param>
  40. /// <param name="toXPath">范例: @"Skill/First/SkillItem"</param>
  41. /// <returns></returns>
  42. public static bool AppendChild(string filePath, string xPath, string toFilePath, string toXPath)
  43. {
  44. try
  45. {
  46. XmlDocument doc = new XmlDocument();
  47. doc.Load(toFilePath);
  48. XmlNode xn = doc.SelectSingleNode(toXPath);
  49. XmlNodeList xnList = ReadNodes(filePath, xPath);
  50. if (xnList != null)
  51. {
  52. foreach (XmlElement xe in xnList)
  53. {
  54. XmlNode n = doc.ImportNode(xe, true);
  55. xn.AppendChild(n);
  56. }
  57. doc.Save(toFilePath);
  58. }
  59. return true;
  60. }
  61. catch
  62. {
  63. return false;
  64. }
  65. }
  66. /// <summary>
  67. /// 修改节点的InnerText的值
  68. /// </summary>
  69. /// <param name="filePath">XML文件绝对路径</param>
  70. /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
  71. /// <param name="value">节点的值</param>
  72. /// <returns></returns>
  73. public static bool UpdateNodeInnerText(string filePath, string xPath, string value)
  74. {
  75. try
  76. {
  77. XmlDocument doc = new XmlDocument();
  78. doc.Load(filePath);
  79. XmlNode xn = doc.SelectSingleNode(xPath);
  80. XmlElement xe = (XmlElement)xn;
  81. xe.InnerText = value;
  82. doc.Save(filePath);
  83. }
  84. catch
  85. {
  86. return false;
  87. }
  88. return true;
  89. }
  90. /// <summary>
  91. /// 读取XML文档
  92. /// </summary>
  93. /// <param name="filePath">XML文件绝对路径</param>
  94. /// <returns></returns>
  95. public static XmlDocument LoadXmlDoc(string filePath)
  96. {
  97. try
  98. {
  99. XmlDocument doc = new XmlDocument();
  100. doc.Load(filePath);
  101. return doc;
  102. }
  103. catch
  104. {
  105. return null;
  106. }
  107. }
  108. #endregion 增、删、改操作
  109. #region 扩展方法===================================================
  110. /// <summary>
  111. /// 读取XML的所有子节点
  112. /// </summary>
  113. /// <param name="filePath">XML文件绝对路径</param>
  114. /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
  115. /// <returns></returns>
  116. public static XmlNodeList ReadNodes(string filePath, string xPath)
  117. {
  118. try
  119. {
  120. XmlDocument doc = new XmlDocument();
  121. doc.Load(filePath);
  122. XmlNode xn = doc.SelectSingleNode(xPath);
  123. XmlNodeList xnList = xn.ChildNodes; //得到该节点的子节点
  124. return xnList;
  125. }
  126. catch
  127. {
  128. return null;
  129. }
  130. }
  131. #endregion 扩展方法
  132. }
  133. }