1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Xml;
  6.  
  7. namespace CommonUtil
  8. {
  9. /// <summary>
  10. /// Xml通用操作类
  11. /// </summary>
  12. public class XmlHelper
  13. {
  14. public XmlHelper()
  15. {
  16. }
  17.  
  18. /// <summary>
  19. /// 创建XML文件
  20. /// </summary>
  21. /// <param name="path">路径</param>
  22. /// <param name="filename">xml文件名称(包含后缀名)</param>
  23. /// <param name="rootname">根节点名称</param>
  24. /**************************************************
  25. * 使用示列:
  26. * XmlHelper.CreateXmlFile(path, "text.xml", "root")
  27. ************************************************/
  28. public static void CreateXmlFile(string path, string filename, string rootname)
  29. {
  30. try
  31. {
  32. if (path.Equals(""))
  33. throw new Exception("路径不能为空!");
  34.  
  35. if (filename.Equals(""))
  36. throw new Exception("文件名称不能为空!");
  37.  
  38. if (!filename.Split('.').GetValue(filename.Split('.').Length - 1).ToString().ToLower().Equals("xml"))
  39. throw new Exception("文件后缀名称必须为xml!");
  40.  
  41. if (rootname.Equals(""))
  42. throw new Exception("根节点名称不能为空!");
  43.  
  44. if (!Directory.Exists(path)) //如果路径不存在,则创建路径
  45. Directory.CreateDirectory(path);
  46.  
  47. StringBuilder builder = new StringBuilder();
  48. builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  49. if (!rootname.Equals(""))
  50. {
  51. builder.AppendLine("<" + rootname + ">");
  52. builder.AppendLine("</" + rootname + ">");
  53. }
  54.  
  55. string fullpath = string.Empty;
  56. XmlDocument doc = new XmlDocument();
  57. doc.LoadXml(builder.ToString());
  58. if (path.LastIndexOf('\\') == path.Length - 1)
  59. fullpath = path + filename;
  60. else
  61. fullpath = path + "\\" + filename;
  62. doc.Save(fullpath); //保存xml文件
  63. }
  64. catch
  65. {
  66. throw;
  67. }
  68. }
  69.  
  70. /// <summary>
  71. /// 读取数据
  72. /// </summary>
  73. /// <param name="path">全路径</param>
  74. /// <param name="node">节点</param>
  75. /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
  76. /// <returns>string</returns>
  77. /**************************************************
  78. * 使用示列:
  79. * XmlHelper.Read(path, "/Node", "")
  80. * XmlHelper.Read(path, "/Node/Element[@Attribute='Value']", "Attribute")
  81. ************************************************/
  82. public static string Read(string path, string node, string attribute)
  83. {
  84. string value = "";
  85. try
  86. {
  87. if (!File.Exists(path))
  88. throw new Exception("xml文件不存在!");
  89.  
  90. if (node.Equals(""))
  91. throw new Exception("节点名称不能为空!");
  92.  
  93. XmlDocument doc = new XmlDocument();
  94. doc.Load(path);
  95. XmlNode xn = doc.SelectSingleNode(node);
  96. value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
  97. }
  98. catch
  99. {
  100. throw;
  101. }
  102. return value;
  103. }
  104.  
  105. /// <summary>
  106. /// 读取数据
  107. /// </summary>
  108. /// <param name="path">全路径</param>
  109. /// <param name="node">节点</param>
  110. /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
  111. /// <returns></returns>
  112. public static List<string> ReadToList(string path, string node, string attribute)
  113. {
  114. List<string> nodelist = new List<string>();
  115.  
  116. try
  117. {
  118. if (!File.Exists(path))
  119. throw new Exception("xml文件不存在!");
  120.  
  121. if (node.Equals(""))
  122. throw new Exception("节点名称不能为空!");
  123.  
  124. XmlDocument doc = new XmlDocument();
  125. doc.Load(path);
  126. XmlNodeList nodes = doc.SelectNodes(node);
  127. if (nodes != null && nodes.Count > 0)
  128. {
  129. foreach (XmlNode n in nodes)
  130. {
  131. if (attribute.Equals(""))
  132. nodelist.Add(n.InnerText);
  133. else
  134. nodelist.Add(n.Attributes[attribute].Value);
  135. }
  136. }
  137. else
  138. {
  139. nodelist = null;
  140. }
  141. }
  142. catch
  143. {
  144. throw;
  145. }
  146.  
  147. return nodelist;
  148. }
  149.  
  150. /// <summary>
  151. /// 插入数据
  152. /// </summary>
  153. /// <param name="path">全路径</param>
  154. /// <param name="node">节点</param>
  155. /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
  156. /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
  157. /// <param name="value">值</param>
  158. /// <returns></returns>
  159. /**************************************************
  160. * 使用示列:
  161. * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
  162. * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
  163. * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
  164. * XmlHelper.Insert(path, "/Node/Element[@Attribute='Value']", "Element", "Attribute", "Value");
  165. ************************************************/
  166. public static void Insert(string path, string node, string element, string attribute, string value)
  167. {
  168. try
  169. {
  170. if (!File.Exists(path))
  171. throw new Exception("xml文件不存在!");
  172.  
  173. if (node.Equals(""))
  174. throw new Exception("节点名称不能为空!");
  175.  
  176. if (element.Equals("") && attribute.Equals(""))
  177. throw new Exception("元素名和属性名不能同时为空!");
  178.  
  179. XmlDocument doc = new XmlDocument();
  180. doc.Load(path);
  181. XmlNode xn = doc.SelectSingleNode(node);
  182. if (element.Equals(""))
  183. {
  184. if (!attribute.Equals(""))
  185. {
  186. XmlElement xe = (XmlElement)xn;
  187. xe.SetAttribute(attribute, value);
  188. }
  189. }
  190. else
  191. {
  192. XmlElement xe = doc.CreateElement(element);
  193. if (attribute.Equals(""))
  194. xe.InnerText = value;
  195. else
  196. xe.SetAttribute(attribute, value);
  197. xn.AppendChild(xe);
  198. }
  199. doc.Save(path);
  200. }
  201. catch
  202. {
  203. throw;
  204. }
  205. }
  206.  
  207. /// <summary>
  208. /// 修改数据
  209. /// </summary>
  210. /// <param name="path">全路径</param>
  211. /// <param name="node">节点</param>
  212. /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
  213. /// <param name="value">值</param>
  214. /// <returns></returns>
  215. /**************************************************
  216. * 使用示列:
  217. * XmlHelper.Insert(path, "/Node", "", "Value")
  218. * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
  219. ************************************************/
  220. public static void Update(string path, string node, string attribute, string value)
  221. {
  222. try
  223. {
  224. if (!File.Exists(path))
  225. throw new Exception("xml文件不存在!");
  226.  
  227. if (node.Equals(""))
  228. throw new Exception("节点名称不能为空!");
  229.  
  230. XmlDocument doc = new XmlDocument();
  231. doc.Load(path);
  232. XmlNode xn = doc.SelectSingleNode(node);
  233. XmlElement xe = (XmlElement)xn;
  234. if (attribute.Equals(""))
  235. xe.InnerText = value;
  236. else
  237. xe.SetAttribute(attribute, value);
  238. doc.Save(path);
  239. }
  240. catch
  241. {
  242. throw;
  243. }
  244. }
  245.  
  246. /// <summary>
  247. /// 删除数据
  248. /// </summary>
  249. /// <param name="path">全路径</param>
  250. /// <param name="node">节点</param>
  251. /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
  252. /// <param name="value">值</param>
  253. /// <returns></returns>
  254. /**************************************************
  255. * 使用示列:
  256. * XmlHelper.Delete(path, "/Node", "")
  257. * XmlHelper.Delete(path, "/Node", "Attribute")
  258. ************************************************/
  259. public static void Delete(string path, string node, string attribute)
  260. {
  261. try
  262. {
  263. if (!File.Exists(path))
  264. throw new Exception("xml文件不存在!");
  265.  
  266. if (node.Equals(""))
  267. throw new Exception("节点名称不能为空!");
  268.  
  269. XmlDocument doc = new XmlDocument();
  270. doc.Load(path);
  271. XmlNodeList nodes = doc.SelectNodes(node);
  272. if (nodes != null && nodes.Count > 0)
  273. {
  274. foreach (XmlNode xn in nodes)
  275. {
  276. XmlElement xe = (XmlElement)xn;
  277. if (attribute.Equals(""))
  278. xn.ParentNode.RemoveChild(xn);
  279. else
  280. xe.RemoveAttribute(attribute);
  281. }
  282. doc.Save(path);
  283. }
  284. }
  285. catch
  286. {
  287. throw;
  288. }
  289. }
  290. }
  291. }

  

Xml通用操作类的更多相关文章

  1. XML文件操作类--创建XML文件

    这个类是在微软XML操作类库上进行的封装,只是为了更加简单使用,包括XML类创建节点的示例. using System; using System.Collections; using System. ...

  2. PHP对XML文件操作类讲解

    <?phpclass XML{    private $dom;        function __construct ()    {        $this->dom = new D ...

  3. C# XML文件操作类XmlHelper

    类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...

  4. [No0000DE]C# XmlHelper XML类型操作 类封装

    using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...

  5. 通用数据库操作类,前端easyui-datagrid,form

    实现功能:     左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...

  6. 简单的XML操作类

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  7. XML转换为对象操作类详解

    //XML转换为对象操作类 //一,XML与Object转换类 using System.IO; using System.Runtime.Serialization.Formatters.Binar ...

  8. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  9. XML Helper XML操作类

    写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...

随机推荐

  1. Css基础-派生选择器

    如果要修改li strong 里面文字的颜色可以这样写样式 派生选择器: li strong { color:red; } 效果:

  2. 数学之路(3)-机器学习(3)-机器学习算法-SVM[5]

    svm小结 1.超平面 两种颜色的点分别代表两个类别,红颜色的线表示一个可行的超平面.在进行分类的时候,我们将数据点  x 代入  f(x)  中,如果得到的结果小于 0 ,则赋予其类别 -1 ,如果 ...

  3. Php AES加密、解密与Java互操作的问题

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  4. 【转】C++对象内存分配问题

    原文:http://blog.csdn.net/c504665913/article/details/7797859 如果一个人自称为程序高手,却对内存一无所知,那么我可以告诉你,他一定在吹牛.用C或 ...

  5. CRM-BP相关FUNCTION

    获取BP的地址信息:BUPA_ADDRESS_GET_DETAIL 修改BP的信息:CRM_WAP_BP_CHANGE BUTO50存放2个BP之间的关系 获取BP的角色 BAPI_BUPA_ROLE ...

  6. hdu 1712 ACboy needs your help 分组背包

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...

  7. java_jdbc_反射

    package cn.itcast.Reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; imp ...

  8. mac mysql error You must reset your password using ALTER USER statement before executing this statement.

    安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个 step 1: SET PASSWORD = PASSWORD('your new password'); step 2: ALTER ...

  9. [转]让你提升命令行效率的 Bash 快捷键

    生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...

  10. 理解hadoop的Map-Reduce数据流(data flow)

    http://blog.csdn.net/yclzh0522/article/details/6859778 Map-Reduce的处理过程主要涉及以下四个部分: 客户端Client:用于提交Map- ...