标签:
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; namespace CommonUtil { /// <summary> /// Xml通用操作类 /// </summary> public class XmlHelper { public XmlHelper() { } /// <summary> /// 创建XML文件 /// </summary> /// <param name="path">路径</param> /// <param name="filename">xml文件名称(包含后缀名)</param> /// <param name="rootname">根节点名称</param> /************************************************** * 使用示列: * 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("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); if (!rootname.Equals("")) { builder.AppendLine("<" + rootname + ">"); builder.AppendLine("</" + rootname + ">"); } 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; } } /// <summary> /// 读取数据 /// </summary> /// <param name="path">全路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// <returns>string</returns> /************************************************** * 使用示列: * 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; } /// <summary> /// 读取数据 /// </summary> /// <param name="path">全路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// <returns></returns> public static List<string> ReadToList(string path, string node, string attribute) { List<string> nodelist = new List<string>(); 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; } /// <summary> /// 插入数据 /// </summary> /// <param name="path">全路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * 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; } } /// <summary> /// 修改数据 /// </summary> /// <param name="path">全路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * 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; } } /// <summary> /// 删除数据 /// </summary> /// <param name="path">全路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * 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; } } } }
标签:
原文地址:http://www.cnblogs.com/yuanfang2015/p/4882365.html