查看: 359|回复: 0

[.NET开发] C# XML操作类分享

发表于 2018-2-9 08:00:00

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

XmlHelper

  1. using System.Xml;
  2. using System.Data;
  3. namespace DotNet.Utilities
  4. {
  5. /// <summary>
  6. /// Xml的操作公共类
  7. /// </summary>
  8. public class XmlHelper
  9. {
  10. #region 字段定义
  11. /// <summary>
  12. /// XML文件的物理路径
  13. /// </summary>
  14. private string _filePath = string.Empty;
  15. /// <summary>
  16. /// Xml文档
  17. /// </summary>
  18. private XmlDocument _xml;
  19. /// <summary>
  20. /// XML的根节点
  21. /// </summary>
  22. private XmlElement _element;
  23. #endregion
  24. #region 构造方法
  25. /// <summary>
  26. /// 实例化XmlHelper对象
  27. /// </summary>
  28. /// <param name="xmlFilePath">Xml文件的相对路径</param>
  29. public XmlHelper(string xmlFilePath)
  30. {
  31. //获取XML文件的绝对路径
  32. _filePath = SysHelper.GetPath(xmlFilePath);
  33. }
  34. #endregion
  35. #region 创建XML的根节点
  36. /// <summary>
  37. /// 创建XML的根节点
  38. /// </summary>
  39. private void CreateXMLElement()
  40. {
  41. //创建一个XML对象
  42. _xml = new XmlDocument();
  43. if (DirFile.IsExistFile(_filePath))
  44. {
  45. //加载XML文件
  46. _xml.Load(this._filePath);
  47. }
  48. //为XML的根节点赋值
  49. _element = _xml.DocumentElement;
  50. }
  51. #endregion
  52. #region 获取指定XPath表达式的节点对象
  53. /// <summary>
  54. /// 获取指定XPath表达式的节点对象
  55. /// </summary>
  56. /// <param name="xPath">XPath表达式,
  57. /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
  58. /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
  59. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
  60. /// </param>
  61. public XmlNode GetNode(string xPath)
  62. {
  63. //创建XML的根节点
  64. CreateXMLElement();
  65. //返回XPath节点
  66. return _element.SelectSingleNode(xPath);
  67. }
  68. #endregion
  69. #region 获取指定XPath表达式节点的值
  70. /// <summary>
  71. /// 获取指定XPath表达式节点的值
  72. /// </summary>
  73. /// <param name="xPath">XPath表达式,
  74. /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
  75. /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
  76. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
  77. /// </param>
  78. public string GetValue(string xPath)
  79. {
  80. //创建XML的根节点
  81. CreateXMLElement();
  82. //返回XPath节点的值
  83. return _element.SelectSingleNode(xPath).InnerText;
  84. }
  85. #endregion
  86. #region 获取指定XPath表达式节点的属性值
  87. /// <summary>
  88. /// 获取指定XPath表达式节点的属性值
  89. /// </summary>
  90. /// <param name="xPath">XPath表达式,
  91. /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
  92. /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
  93. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
  94. /// </param>
  95. /// <param name="attributeName">属性名</param>
  96. public string GetAttributeValue(string xPath, string attributeName)
  97. {
  98. //创建XML的根节点
  99. CreateXMLElement();
  100. //返回XPath节点的属性值
  101. return _element.SelectSingleNode(xPath).Attributes[attributeName].Value;
  102. }
  103. #endregion
  104. #region 新增节点
  105. /// <summary>
  106. /// 1. 功能:新增节点。
  107. /// 2. 使用条件:将任意节点插入到当前Xml文件中。
  108. /// </summary>
  109. /// <param name="xmlNode">要插入的Xml节点</param>
  110. public void AppendNode(XmlNode xmlNode)
  111. {
  112. //创建XML的根节点
  113. CreateXMLElement();
  114. //导入节点
  115. XmlNode node = _xml.ImportNode(xmlNode, true);
  116. //将节点插入到根节点下
  117. _element.AppendChild(node);
  118. }
  119. /// <summary>
  120. /// 1. 功能:新增节点。
  121. /// 2. 使用条件:将DataSet中的第一条记录插入Xml文件中。
  122. /// </summary>
  123. /// <param name="ds">DataSet的实例,该DataSet中应该只有一条记录</param>
  124. public void AppendNode(DataSet ds)
  125. {
  126. //创建XmlDataDocument对象
  127. XmlDataDocument xmlDataDocument = new XmlDataDocument(ds);
  128. //导入节点
  129. XmlNode node = xmlDataDocument.DocumentElement.FirstChild;
  130. //将节点插入到根节点下
  131. AppendNode(node);
  132. }
  133. #endregion
  134. #region 删除节点
  135. /// <summary>
  136. /// 删除指定XPath表达式的节点
  137. /// </summary>
  138. /// <param name="xPath">XPath表达式,
  139. /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
  140. /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
  141. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
  142. /// </param>
  143. public void RemoveNode(string xPath)
  144. {
  145. //创建XML的根节点
  146. CreateXMLElement();
  147. //获取要删除的节点
  148. XmlNode node = _xml.SelectSingleNode(xPath);
  149. //删除节点
  150. _element.RemoveChild(node);
  151. }
  152. #endregion //删除节点
  153. #region 保存XML文件
  154. /// <summary>
  155. /// 保存XML文件
  156. /// </summary>
  157. public void Save()
  158. {
  159. //创建XML的根节点
  160. CreateXMLElement();
  161. //保存XML文件
  162. _xml.Save(this._filePath);
  163. }
  164. #endregion //保存XML文件
  165. #region 静态方法
  166. #region 创建根节点对象
  167. /// <summary>
  168. /// 创建根节点对象
  169. /// </summary>
  170. /// <param name="xmlFilePath">Xml文件的相对路径</param>
  171. private static XmlElement CreateRootElement(string xmlFilePath)
  172. {
  173. //定义变量,表示XML文件的绝对路径
  174. string filePath = "";
  175. //获取XML文件的绝对路径
  176. filePath = SysHelper.GetPath(xmlFilePath);
  177. //创建XmlDocument对象
  178. XmlDocument xmlDocument = new XmlDocument();
  179. //加载XML文件
  180. xmlDocument.Load(filePath);
  181. //返回根节点
  182. return xmlDocument.DocumentElement;
  183. }
  184. #endregion
  185. #region 获取指定XPath表达式节点的值
  186. /// <summary>
  187. /// 获取指定XPath表达式节点的值
  188. /// </summary>
  189. /// <param name="xmlFilePath">Xml文件的相对路径</param>
  190. /// <param name="xPath">XPath表达式,
  191. /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
  192. /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
  193. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
  194. /// </param>
  195. public static string GetValue(string xmlFilePath, string xPath)
  196. {
  197. //创建根对象
  198. XmlElement rootElement = CreateRootElement(xmlFilePath);
  199. //返回XPath节点的值
  200. return rootElement.SelectSingleNode(xPath).InnerText;
  201. }
  202. #endregion
  203. #region 获取指定XPath表达式节点的属性值
  204. /// <summary>
  205. /// 获取指定XPath表达式节点的属性值
  206. /// </summary>
  207. /// <param name="xmlFilePath">Xml文件的相对路径</param>
  208. /// <param name="xPath">XPath表达式,
  209. /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
  210. /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
  211. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
  212. /// </param>
  213. /// <param name="attributeName">属性名</param>
  214. public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName)
  215. {
  216. //创建根对象
  217. XmlElement rootElement = CreateRootElement(xmlFilePath);
  218. //返回XPath节点的属性值
  219. return rootElement.SelectSingleNode(xPath).Attributes[attributeName].Value;
  220. }
  221. #endregion
  222. #endregion
  223. public static void SetValue(string xmlFilePath, string xPath, string newtext)
  224. {
  225. //string path = SysHelper.GetPath(xmlFilePath);
  226. //var queryXML = from xmlLog in xelem.Descendants("msg_log")
  227. // //所有名字为Bin的记录
  228. // where xmlLog.Element("user").Value == "Bin"
  229. // select xmlLog;
  230. //foreach (XElement el in queryXML)
  231. //{
  232. // el.Element("user").Value = "LiuBin";//开始修改
  233. //}
  234. //xelem.Save(path);
  235. }
  236. }
  237. }
复制代码

XmlProcess

  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Xml;
  5. namespace DotNet.Utilities
  6. {
  7. public class XMLProcess
  8. {
  9. #region 构造函数
  10. public XMLProcess()
  11. { }
  12. public XMLProcess(string strPath)
  13. {
  14. this._XMLPath = strPath;
  15. }
  16. #endregion
  17. #region 公有属性
  18. private string _XMLPath;
  19. public string XMLPath
  20. {
  21. get { return this._XMLPath; }
  22. }
  23. #endregion
  24. #region 私有方法
  25. /// <summary>
  26. /// 导入XML文件
  27. /// </summary>
  28. /// <param name="XMLPath">XML文件路径</param>
  29. private XmlDocument XMLLoad()
  30. {
  31. string XMLFile = XMLPath;
  32. XmlDocument xmldoc = new XmlDocument();
  33. try
  34. {
  35. string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile;
  36. if (File.Exists(filename)) xmldoc.Load(filename);
  37. }
  38. catch (Exception e)
  39. { }
  40. return xmldoc;
  41. }
  42. /// <summary>
  43. /// 导入XML文件
  44. /// </summary>
  45. /// <param name="XMLPath">XML文件路径</param>
  46. private static XmlDocument XMLLoad(string strPath)
  47. {
  48. XmlDocument xmldoc = new XmlDocument();
  49. try
  50. {
  51. string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + strPath;
  52. if (File.Exists(filename)) xmldoc.Load(filename);
  53. }
  54. catch (Exception e)
  55. { }
  56. return xmldoc;
  57. }
  58. /// <summary>
  59. /// 返回完整路径
  60. /// </summary>
  61. /// <param name="strPath">Xml的路径</param>
  62. private static string GetXmlFullPath(string strPath)
  63. {
  64. if (strPath.IndexOf(":") > 0)
  65. {
  66. return strPath;
  67. }
  68. else
  69. {
  70. return System.Web.HttpContext.Current.Server.MapPath(strPath);
  71. }
  72. }
  73. #endregion
  74. #region 读取数据
  75. /// <summary>
  76. /// 读取指定节点的数据
  77. /// </summary>
  78. /// <param name="node">节点</param>
  79. /// 使用示列:
  80. /// XMLProsess.Read("/Node", "")
  81. /// XMLProsess.Read("/Node/Element[@Attribute='Name']")
  82. public string Read(string node)
  83. {
  84. string value = "";
  85. try
  86. {
  87. XmlDocument doc = XMLLoad();
  88. XmlNode xn = doc.SelectSingleNode(node);
  89. value = xn.InnerText;
  90. }
  91. catch { }
  92. return value;
  93. }
  94. /// <summary>
  95. /// 读取指定路径和节点的串联值
  96. /// </summary>
  97. /// <param name="path">路径</param>
  98. /// <param name="node">节点</param>
  99. /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
  100. /// 使用示列:
  101. /// XMLProsess.Read(path, "/Node", "")
  102. /// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']")
  103. public static string Read(string path, string node)
  104. {
  105. string value = "";
  106. try
  107. {
  108. XmlDocument doc = XMLLoad(path);
  109. XmlNode xn = doc.SelectSingleNode(node);
  110. value = xn.InnerText;
  111. }
  112. catch { }
  113. return value;
  114. }
  115. /// <summary>
  116. /// 读取指定路径和节点的属性值
  117. /// </summary>
  118. /// <param name="path">路径</param>
  119. /// <param name="node">节点</param>
  120. /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
  121. /// 使用示列:
  122. /// XMLProsess.Read(path, "/Node", "")
  123. /// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
  124. public static string Read(string path, string node, string attribute)
  125. {
  126. string value = "";
  127. try
  128. {
  129. XmlDocument doc = XMLLoad(path);
  130. XmlNode xn = doc.SelectSingleNode(node);
  131. value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
  132. }
  133. catch { }
  134. return value;
  135. }
  136. /// <summary>
  137. /// 获取某一节点的所有孩子节点的值
  138. /// </summary>
  139. /// <param name="node">要查询的节点</param>
  140. public string[] ReadAllChildallValue(string node)
  141. {
  142. int i = 0;
  143. string[] str = { };
  144. XmlDocument doc = XMLLoad();
  145. XmlNode xn = doc.SelectSingleNode(node);
  146. XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点
  147. if (nodelist.Count > 0)
  148. {
  149. str = new string[nodelist.Count];
  150. foreach (XmlElement el in nodelist)//读元素值
  151. {
  152. str[i] = el.Value;
  153. i++;
  154. }
  155. }
  156. return str;
  157. }
  158. /// <summary>
  159. /// 获取某一节点的所有孩子节点的值
  160. /// </summary>
  161. /// <param name="node">要查询的节点</param>
  162. public XmlNodeList ReadAllChild(string node)
  163. {
  164. XmlDocument doc = XMLLoad();
  165. XmlNode xn = doc.SelectSingleNode(node);
  166. XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点
  167. return nodelist;
  168. }
  169. /// <summary>
  170. /// 读取XML返回经排序或筛选后的DataView
  171. /// </summary>
  172. /// <param name="strWhere">筛选条件,如:"name='kgdiwss'"</param>
  173. /// <param name="strSort"> 排序条件,如:"Id desc"</param>
  174. public DataView GetDataViewByXml(string strWhere, string strSort)
  175. {
  176. try
  177. {
  178. string XMLFile = this.XMLPath;
  179. string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile;
  180. DataSet ds = new DataSet();
  181. ds.ReadXml(filename);
  182. DataView dv = new DataView(ds.Tables[0]); //创建DataView来完成排序或筛选操作
  183. if (strSort != null)
  184. {
  185. dv.Sort = strSort; //对DataView中的记录进行排序
  186. }
  187. if (strWhere != null)
  188. {
  189. dv.RowFilter = strWhere; //对DataView中的记录进行筛选,找到我们想要的记录
  190. }
  191. return dv;
  192. }
  193. catch (Exception)
  194. {
  195. return null;
  196. }
  197. }
  198. /// <summary>
  199. /// 读取XML返回DataSet
  200. /// </summary>
  201. /// <param name="strXmlPath">XML文件相对路径</param>
  202. public DataSet GetDataSetByXml(string strXmlPath)
  203. {
  204. try
  205. {
  206. DataSet ds = new DataSet();
  207. ds.ReadXml(GetXmlFullPath(strXmlPath));
  208. if (ds.Tables.Count > 0)
  209. {
  210. return ds;
  211. }
  212. return null;
  213. }
  214. catch (Exception)
  215. {
  216. return null;
  217. }
  218. }
  219. #endregion
  220. #region 插入数据
  221. /// <summary>
  222. /// 插入数据
  223. /// </summary>
  224. /// <param name="path">路径</param>
  225. /// <param name="node">节点</param>
  226. /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
  227. /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
  228. /// <param name="value">值</param>
  229. /// 使用示列:
  230. /// XMLProsess.Insert(path, "/Node", "Element", "", "Value")
  231. /// XMLProsess.Insert(path, "/Node", "Element", "Attribute", "Value")
  232. /// XMLProsess.Insert(path, "/Node", "", "Attribute", "Value")
  233. public static void Insert(string path, string node, string element, string attribute, string value)
  234. {
  235. try
  236. {
  237. XmlDocument doc = new XmlDocument();
  238. doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  239. XmlNode xn = doc.SelectSingleNode(node);
  240. if (element.Equals(""))
  241. {
  242. if (!attribute.Equals(""))
  243. {
  244. XmlElement xe = (XmlElement)xn;
  245. xe.SetAttribute(attribute, value);
  246. }
  247. }
  248. else
  249. {
  250. XmlElement xe = doc.CreateElement(element);
  251. if (attribute.Equals(""))
  252. xe.InnerText = value;
  253. else
  254. xe.SetAttribute(attribute, value);
  255. xn.AppendChild(xe);
  256. }
  257. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  258. }
  259. catch { }
  260. }
  261. /// <summary>
  262. /// 插入数据
  263. /// </summary>
  264. /// <param name="path">路径</param>
  265. /// <param name="node">节点</param>
  266. /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
  267. /// <param name="strList">由XML属性名和值组成的二维数组</param>
  268. public static void Insert(string path, string node, string element, string[][] strList)
  269. {
  270. try
  271. {
  272. XmlDocument doc = new XmlDocument();
  273. doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  274. XmlNode xn = doc.SelectSingleNode(node);
  275. XmlElement xe = doc.CreateElement(element);
  276. string strAttribute = "";
  277. string strValue = "";
  278. for (int i = 0; i < strList.Length; i++)
  279. {
  280. for (int j = 0; j < strList[i].Length; j++)
  281. {
  282. if (j == 0)
  283. strAttribute = strList[i][j];
  284. else
  285. strValue = strList[i][j];
  286. }
  287. if (strAttribute.Equals(""))
  288. xe.InnerText = strValue;
  289. else
  290. xe.SetAttribute(strAttribute, strValue);
  291. }
  292. xn.AppendChild(xe);
  293. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  294. }
  295. catch { }
  296. }
  297. /// <summary>
  298. /// 插入一行数据
  299. /// </summary>
  300. /// <param name="strXmlPath">XML文件相对路径</param>
  301. /// <param name="Columns">要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};</param>
  302. /// <param name="ColumnValue">要插入行每列的值数组,如:string[] ColumnValue={"XML大全","false"};</param>
  303. /// <returns>成功返回true,否则返回false</returns>
  304. public static bool WriteXmlByDataSet(string strXmlPath, string[] Columns, string[] ColumnValue)
  305. {
  306. try
  307. {
  308. //根据传入的XML路径得到.XSD的路径,两个文件放在同一个目录下
  309. string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd";
  310. DataSet ds = new DataSet();
  311. ds.ReadXmlSchema(GetXmlFullPath(strXsdPath)); //读XML架构,关系到列的数据类型
  312. ds.ReadXml(GetXmlFullPath(strXmlPath));
  313. DataTable dt = ds.Tables[0];
  314. DataRow newRow = dt.NewRow(); //在原来的表格基础上创建新行
  315. for (int i = 0; i < Columns.Length; i++) //循环给一行中的各个列赋值
  316. {
  317. newRow[Columns[i]] = ColumnValue[i];
  318. }
  319. dt.Rows.Add(newRow);
  320. dt.AcceptChanges();
  321. ds.AcceptChanges();
  322. ds.WriteXml(GetXmlFullPath(strXmlPath));
  323. return true;
  324. }
  325. catch (Exception)
  326. {
  327. return false;
  328. }
  329. }
  330. #endregion
  331. #region 修改数据
  332. /// <summary>
  333. /// 修改指定节点的数据
  334. /// </summary>
  335. /// <param name="node">节点</param>
  336. /// <param name="value">值</param>
  337. public void Update(string node, string value)
  338. {
  339. try
  340. {
  341. XmlDocument doc = XMLLoad();
  342. XmlNode xn = doc.SelectSingleNode(node);
  343. xn.InnerText = value;
  344. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLPath);
  345. }
  346. catch { }
  347. }
  348. /// <summary>
  349. /// 修改指定节点的数据
  350. /// </summary>
  351. /// <param name="path">路径</param>
  352. /// <param name="node">节点</param>
  353. /// <param name="value">值</param>
  354. /// 使用示列:
  355. /// XMLProsess.Insert(path, "/Node","Value")
  356. /// XMLProsess.Insert(path, "/Node","Value")
  357. public static void Update(string path, string node, string value)
  358. {
  359. try
  360. {
  361. XmlDocument doc = XMLLoad(path);
  362. XmlNode xn = doc.SelectSingleNode(node);
  363. xn.InnerText = value;
  364. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  365. }
  366. catch { }
  367. }
  368. /// <summary>
  369. /// 修改指定节点的属性值(静态)
  370. /// </summary>
  371. /// <param name="path">路径</param>
  372. /// <param name="node">节点</param>
  373. /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
  374. /// <param name="value">值</param>
  375. /// 使用示列:
  376. /// XMLProsess.Insert(path, "/Node", "", "Value")
  377. /// XMLProsess.Insert(path, "/Node", "Attribute", "Value")
  378. public static void Update(string path, string node, string attribute, string value)
  379. {
  380. try
  381. {
  382. XmlDocument doc = XMLLoad(path);
  383. XmlNode xn = doc.SelectSingleNode(node);
  384. XmlElement xe = (XmlElement)xn;
  385. if (attribute.Equals(""))
  386. xe.InnerText = value;
  387. else
  388. xe.SetAttribute(attribute, value);
  389. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  390. }
  391. catch { }
  392. }
  393. /// <summary>
  394. /// 更改符合条件的一条记录
  395. /// </summary>
  396. /// <param name="strXmlPath">XML文件路径</param>
  397. /// <param name="Columns">列名数组</param>
  398. /// <param name="ColumnValue">列值数组</param>
  399. /// <param name="strWhereColumnName">条件列名</param>
  400. /// <param name="strWhereColumnValue">条件列值</param>
  401. public static bool UpdateXmlRow(string strXmlPath, string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue)
  402. {
  403. try
  404. {
  405. string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd";
  406. DataSet ds = new DataSet();
  407. ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));//读XML架构,关系到列的数据类型
  408. ds.ReadXml(GetXmlFullPath(strXmlPath));
  409. //先判断行数
  410. if (ds.Tables[0].Rows.Count > 0)
  411. {
  412. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  413. {
  414. //如果当前记录为符合Where条件的记录
  415. if (ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue))
  416. {
  417. //循环给找到行的各列赋新值
  418. for (int j = 0; j < Columns.Length; j++)
  419. {
  420. ds.Tables[0].Rows[i][Columns[j]] = ColumnValue[j];
  421. }
  422. ds.AcceptChanges(); //更新DataSet
  423. ds.WriteXml(GetXmlFullPath(strXmlPath));//重新写入XML文件
  424. return true;
  425. }
  426. }
  427. }
  428. return false;
  429. }
  430. catch (Exception)
  431. {
  432. return false;
  433. }
  434. }
  435. #endregion
  436. #region 删除数据
  437. /// <summary>
  438. /// 删除节点值
  439. /// </summary>
  440. /// <param name="path">路径</param>
  441. /// <param name="node">节点</param>
  442. /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
  443. /// <param name="value">值</param>
  444. /// 使用示列:
  445. /// XMLProsess.Delete(path, "/Node", "")
  446. /// XMLProsess.Delete(path, "/Node", "Attribute")
  447. public static void Delete(string path, string node)
  448. {
  449. try
  450. {
  451. XmlDocument doc = XMLLoad(path);
  452. XmlNode xn = doc.SelectSingleNode(node);
  453. xn.ParentNode.RemoveChild(xn);
  454. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  455. }
  456. catch { }
  457. }
  458. /// <summary>
  459. /// 删除数据
  460. /// </summary>
  461. /// <param name="path">路径</param>
  462. /// <param name="node">节点</param>
  463. /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
  464. /// <param name="value">值</param>
  465. /// 使用示列:
  466. /// XMLProsess.Delete(path, "/Node", "")
  467. /// XMLProsess.Delete(path, "/Node", "Attribute")
  468. public static void Delete(string path, string node, string attribute)
  469. {
  470. try
  471. {
  472. XmlDocument doc = XMLLoad(path);
  473. XmlNode xn = doc.SelectSingleNode(node);
  474. XmlElement xe = (XmlElement)xn;
  475. if (attribute.Equals(""))
  476. xn.ParentNode.RemoveChild(xn);
  477. else
  478. xe.RemoveAttribute(attribute);
  479. doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
  480. }
  481. catch { }
  482. }
  483. /// <summary>
  484. /// 删除所有行
  485. /// </summary>
  486. /// <param name="strXmlPath">XML路径</param>
  487. public static bool DeleteXmlAllRows(string strXmlPath)
  488. {
  489. try
  490. {
  491. DataSet ds = new DataSet();
  492. ds.ReadXml(GetXmlFullPath(strXmlPath));
  493. if (ds.Tables[0].Rows.Count > 0)
  494. {
  495. ds.Tables[0].Rows.Clear();
  496. }
  497. ds.WriteXml(GetXmlFullPath(strXmlPath));
  498. return true;
  499. }
  500. catch (Exception)
  501. {
  502. return false;
  503. }
  504. }
  505. /// <summary>
  506. /// 通过删除DataSet中指定索引行,重写XML以实现删除指定行
  507. /// </summary>
  508. /// <param name="iDeleteRow">要删除的行在DataSet中的Index值</param>
  509. public static bool DeleteXmlRowByIndex(string strXmlPath, int iDeleteRow)
  510. {
  511. try
  512. {
  513. DataSet ds = new DataSet();
  514. ds.ReadXml(GetXmlFullPath(strXmlPath));
  515. if (ds.Tables[0].Rows.Count > 0)
  516. {
  517. ds.Tables[0].Rows[iDeleteRow].Delete();
  518. }
  519. ds.WriteXml(GetXmlFullPath(strXmlPath));
  520. return true;
  521. }
  522. catch (Exception)
  523. {
  524. return false;
  525. }
  526. }
  527. /// <summary>
  528. /// 删除指定列中指定值的行
  529. /// </summary>
  530. /// <param name="strXmlPath">XML相对路径</param>
  531. /// <param name="strColumn">列名</param>
  532. /// <param name="ColumnValue">指定值</param>
  533. public static bool DeleteXmlRows(string strXmlPath, string strColumn, string[] ColumnValue)
  534. {
  535. try
  536. {
  537. DataSet ds = new DataSet();
  538. ds.ReadXml(GetXmlFullPath(strXmlPath));
  539. if (ds.Tables[0].Rows.Count > 0)
  540. {
  541. //判断行多还是删除的值多,多的for循环放在里面
  542. if (ColumnValue.Length > ds.Tables[0].Rows.Count)
  543. {
  544. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  545. {
  546. for (int j = 0; j < ColumnValue.Length; j++)
  547. {
  548. if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
  549. {
  550. ds.Tables[0].Rows[i].Delete();
  551. }
  552. }
  553. }
  554. }
  555. else
  556. {
  557. for (int j = 0; j < ColumnValue.Length; j++)
  558. {
  559. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  560. {
  561. if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
  562. {
  563. ds.Tables[0].Rows[i].Delete();
  564. }
  565. }
  566. }
  567. }
  568. ds.WriteXml(GetXmlFullPath(strXmlPath));
  569. }
  570. return true;
  571. }
  572. catch (Exception)
  573. {
  574. return false;
  575. }
  576. }
  577. #endregion
  578. }
  579. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • C#实现的XML操作类实例
  • C#实现的xml操作类完整实例


回复

使用道具 举报