XMlHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Xml;
  11. using System.Collections.Generic;
  12. namespace Ant.Service.Common
  13. {
  14. /// <summary>
  15. ///XMlHelper 的摘要说明
  16. /// </summary>
  17. public class XMlHelper
  18. {
  19. public XMlHelper()
  20. {
  21. }
  22. /// <summary>
  23. /// 读取数据
  24. /// </summary>
  25. /// <param name="path">路径</param>
  26. /// <param name="node">节点</param>
  27. /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
  28. /// <returns>string</returns>
  29. /**************************************************
  30. * 使用示列:
  31. * XmlHelper.Read(path, "/Node", "")
  32. * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
  33. ************************************************/
  34. public static string Read(string path, string node, string attribute)
  35. {
  36. string value = "";
  37. try
  38. {
  39. XmlDocument doc = new XmlDocument();
  40. doc.Load(path);
  41. XmlNode xn = doc.SelectSingleNode(node);
  42. value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
  43. }
  44. catch { }
  45. return value;
  46. }
  47. ///<summary>
  48. /// 读取配置文件某节点的个数
  49. ///</summary>
  50. ///<param name="path">配置文件的路径</param>
  51. ///<param name="nodeName">要获取的节点</param>
  52. public static Dictionary<string, string> ReadConfig(string path, string nodeName)
  53. {
  54. Dictionary<string, string> dic = new Dictionary<string, string>();
  55. string absoPath = string.Empty; //绝对路径
  56. try
  57. {
  58. absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
  59. if (DirFile.IsExistFiles(absoPath))
  60. {
  61. XmlDocument xd = new XmlDocument();
  62. xd.Load(absoPath);
  63. XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相应节点的集合
  64. if (nodeList != null && nodeList.Count > 0)
  65. for (int i = 0; i < nodeList.Count; i++)
  66. dic.Add(nodeList.Item(i).Attributes["key"].Value, nodeList.Item(i).Attributes["value"].Value);
  67. }
  68. return dic;
  69. }
  70. catch (Exception ex)
  71. {
  72. throw new Exception(ex.Message);
  73. }
  74. }
  75. /// <summary>
  76. /// 插入数据
  77. /// </summary>
  78. /// <param name="path">路径</param>
  79. /// <param name="node">节点</param>
  80. /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
  81. /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
  82. /// <param name="value">值</param>
  83. /// <returns></returns>
  84. /**************************************************
  85. * 使用示列:
  86. * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
  87. * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
  88. * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
  89. ************************************************/
  90. public static void Insert(string path, string node, string element, string attribute, string value)
  91. {
  92. try
  93. {
  94. XmlDocument doc = new XmlDocument();
  95. doc.Load(path);
  96. XmlNode xn = doc.SelectSingleNode(node);
  97. if (element.Equals(""))
  98. {
  99. if (!attribute.Equals(""))
  100. {
  101. XmlElement xe = (XmlElement)xn;
  102. xe.SetAttribute(attribute, value);
  103. }
  104. }
  105. else
  106. {
  107. XmlElement xe = doc.CreateElement(element);
  108. if (attribute.Equals(""))
  109. xe.InnerText = value;
  110. else
  111. xe.SetAttribute(attribute, value);
  112. xn.AppendChild(xe);
  113. }
  114. doc.Save(path);
  115. }
  116. catch
  117. {
  118. }
  119. }
  120. public static void Insertinfo(string path, string node, string element, string attribute, string value)
  121. {
  122. try
  123. {
  124. XmlDocument doc = new XmlDocument();
  125. doc.Load(path);
  126. XmlNode xn = doc.SelectSingleNode(node);
  127. if (element.Equals(""))
  128. {
  129. if (!attribute.Equals(""))
  130. {
  131. XmlElement xe = (XmlElement)xn;
  132. xe.SetAttribute(attribute, value);
  133. }
  134. }
  135. else
  136. {
  137. XmlElement xe = doc.CreateElement(element);
  138. if (attribute.Equals(""))
  139. xe.InnerText = value;
  140. else
  141. xe.SetAttribute(attribute, value);
  142. xn.AppendChild(xe);
  143. }
  144. doc.Save(path);
  145. }
  146. catch
  147. {
  148. }
  149. }
  150. static public void updateinfo(string path, string node, string attribute, string value)
  151. {
  152. try
  153. {
  154. XmlDocument doc = new XmlDocument();
  155. doc.Load(path);
  156. XmlNode xn = doc.SelectSingleNode(node).NextSibling;
  157. XmlCDataSection cdata = (XmlCDataSection)xn;
  158. cdata.InnerText = value;
  159. doc.Save(path);
  160. }
  161. catch
  162. {
  163. }
  164. }
  165. /// <summary>
  166. /// 修改数据
  167. /// </summary>
  168. /// <param name="path">路径</param>
  169. /// <param name="node">节点</param>
  170. /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
  171. /// <param name="value">值</param>
  172. /// <returns></returns>
  173. /**************************************************
  174. * 使用示列:
  175. * XmlHelper.Insert(path, "/Node", "", "Value")
  176. * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
  177. ************************************************/
  178. public static void Update(string path, string node, string attribute, string value)
  179. {
  180. try
  181. {
  182. XmlDocument doc = new XmlDocument();
  183. doc.Load(path);
  184. XmlNode xn = doc.SelectSingleNode(node);
  185. XmlElement xe = (XmlElement)xn;
  186. //XmlCDataSection cdata = (XmlCDataSection)xn;
  187. if (attribute.Equals(""))
  188. xe.InnerText = value;
  189. else
  190. xe.SetAttribute(attribute, value);
  191. doc.Save(path);
  192. }
  193. catch { }
  194. }
  195. /// <summary>
  196. /// 删除数据
  197. /// </summary>
  198. /// <param name="path">路径</param>
  199. /// <param name="node">节点</param>
  200. /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
  201. /// <param name="value">值</param>
  202. /// <returns></returns>
  203. /**************************************************
  204. * 使用示列:
  205. * XmlHelper.Delete(path, "/Node", "")
  206. * XmlHelper.Delete(path, "/Node", "Attribute")
  207. ************************************************/
  208. public static void Delete(string path, string node, string attribute)
  209. {
  210. try
  211. {
  212. XmlDocument doc = new XmlDocument();
  213. doc.Load(path);
  214. XmlNode xn = doc.SelectSingleNode(node);
  215. XmlElement xe = (XmlElement)xn;
  216. if (attribute.Equals(""))
  217. xn.ParentNode.RemoveChild(xn);
  218. else
  219. xe.RemoveAttribute(attribute);
  220. doc.Save(path);
  221. }
  222. catch { }
  223. }
  224. //==================================================
  225. //XmlFile.xml:
  226. //<?xml version="1.0" encoding="utf-8"?>
  227. //<Root />
  228. //==================================================
  229. //使用方法:
  230. //string xml = Server.MapPath("XmlFile.xml");
  231. //插入元素
  232. //XmlHelper.Insert(xml, "/Root", "Studio", "", "");
  233. //插入元素/属性
  234. //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
  235. //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
  236. //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "谱天城工作室");
  237. //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Master", "", "红尘静思");
  238. //插入属性
  239. //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");
  240. //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");
  241. //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", "http://www.putiancheng.com/");
  242. //修改元素值
  243. //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
  244. //修改属性值
  245. //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", "http://www.putiancheng.net/");
  246. //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Name", "PuTianCheng Studio");
  247. //读取元素值
  248. //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>");
  249. //读取属性值
  250. //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
  251. //读取特定属性值
  252. //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "</div>");
  253. //删除属性
  254. //XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");
  255. //删除元素
  256. //XmlHelper.Delete(xml, "/Root/Studio", "");
  257. }
  258. }