using System;using System.Collections.Generic;using System.IO;using System.Text;using System.Xml;namespace CommonUtil{ ////// Xml通用操作类 /// public class XmlHelper { public XmlHelper() { } ////// 创建XML文件 /// /// 路径 /// xml文件名称(包含后缀名) /// 根节点名称 /************************************************** * 使用示列: * XmlHelper.CreateXmlFile(path, "text.xml", "root") ************************************************/ public static void CreateXmlFile(string path, string filename, string rootname) { try { if (path.Equals("")) throw new Exception("路径不能为空!"); if (filename.Equals("")) throw new Exception("文件名称不能为空!"); if (!filename.Split('.').GetValue(filename.Split('.').Length - 1).ToString().ToLower().Equals("xml")) throw new Exception("文件后缀名称必须为xml!"); if (rootname.Equals("")) throw new Exception("根节点名称不能为空!"); if (!Directory.Exists(path)) //如果路径不存在,则创建路径 Directory.CreateDirectory(path); StringBuilder builder = new StringBuilder(); builder.AppendLine(" "); if (!rootname.Equals("")) { builder.AppendLine("<" + rootname + ">"); builder.AppendLine(" "); } string fullpath = string.Empty; XmlDocument doc = new XmlDocument(); doc.LoadXml(builder.ToString()); if (path.LastIndexOf('\\') == path.Length - 1) fullpath = path + filename; else fullpath = path + "\\" + filename; doc.Save(fullpath); //保存xml文件 } catch { throw; } } ////// 读取数据 /// /// 全路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 ///string /************************************************** * 使用示列: * XmlHelper.Read(path, "/Node", "") * XmlHelper.Read(path, "/Node/Element[@Attribute='Value']", "Attribute") ************************************************/ public static string Read(string path, string node, string attribute) { string value = ""; try { if (!File.Exists(path)) throw new Exception("xml文件不存在!"); if (node.Equals("")) throw new Exception("节点名称不能为空!"); XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { throw; } return value; } ////// 读取数据 /// /// 全路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 ///public static List ReadToList(string path, string node, string attribute) { List nodelist = new List (); try { if (!File.Exists(path)) throw new Exception("xml文件不存在!"); if (node.Equals("")) throw new Exception("节点名称不能为空!"); XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNodeList nodes = doc.SelectNodes(node); if (nodes != null && nodes.Count > 0) { foreach (XmlNode n in nodes) { if (attribute.Equals("")) nodelist.Add(n.InnerText); else nodelist.Add(n.Attributes[attribute].Value); } } else { nodelist = null; } } catch { throw; } return nodelist; } /// /// 插入数据 /// /// 全路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 属性名,非空时插入该元素属性值,否则插入元素值 /// 值 ////************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "Element", "", "Value") * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value") * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value") * XmlHelper.Insert(path, "/Node/Element[@Attribute='Value']", "Element", "Attribute", "Value"); ************************************************/ public static void Insert(string path, string node, string element, string attribute, string value) { try { if (!File.Exists(path)) throw new Exception("xml文件不存在!"); if (node.Equals("")) throw new Exception("节点名称不能为空!"); if (element.Equals("") && attribute.Equals("")) throw new Exception("元素名和属性名不能同时为空!"); XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { XmlElement xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { XmlElement xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(path); } catch { throw; } } /// /// 修改数据 /// /// 全路径 /// 节点 /// 属性名,非空时修改该节点属性值,否则修改节点值 /// 值 ////************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "", "Value") * XmlHelper.Insert(path, "/Node", "Attribute", "Value") ************************************************/ public static void Update(string path, string node, string attribute, string value) { try { if (!File.Exists(path)) throw new Exception("xml文件不存在!"); if (node.Equals("")) throw new Exception("节点名称不能为空!"); XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(path); } catch { throw; } } /// /// 删除数据 /// /// 全路径 /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 /// 值 ////************************************************** * 使用示列: * XmlHelper.Delete(path, "/Node", "") * XmlHelper.Delete(path, "/Node", "Attribute") ************************************************/ public static void Delete(string path, string node, string attribute) { try { if (!File.Exists(path)) throw new Exception("xml文件不存在!"); if (node.Equals("")) throw new Exception("节点名称不能为空!"); XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNodeList nodes = doc.SelectNodes(node); if (nodes != null && nodes.Count > 0) { foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); } doc.Save(path); } } catch { throw; } } }}