using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Data; using System.IO; namespace demo.ClassHelper.XML { class XmlHelper { #region 构造函数 public XmlHelper() { } public XmlHelper(string strPath) { this._XMLPath = strPath; } #endregion #region 公有属性 private string _XMLPath; public string XMLPath { get { return this._XMLPath; } } #endregion #region 私有方法 /// /// 导入XML文件 /// /// XML文件路径 private XmlDocument XMLLoad() { string XMLFile = XMLPath; XmlDocument xmldoc = new XmlDocument(); try { string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile; if (File.Exists(filename)) xmldoc.Load(filename); } catch { } return xmldoc; } /// /// 导入XML文件 /// /// XML文件路径 private static XmlDocument XMLLoad(string strPath) { XmlDocument xmldoc = new XmlDocument(); try { string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + strPath; if (File.Exists(filename)) xmldoc.Load(filename); } catch { } return xmldoc; } /// /// 返回完整路径 /// /// Xml的路径 //private static string GetXmlFullPath(string strPath) //{ // if (strPath.IndexOf(":") > 0) // { // return strPath; // } // else // { // return System.Web.HttpContext.Current.Server.MapPath(strPath); // } //} #endregion #region 读取数据 /// /// 读取指定节点的数据 /// /// 节点 /// 使用示列: /// XMLProsess.Read("/Node", "") /// XMLProsess.Read("/Node/Element[@Attribute='Name']") public string Read(string node) { string value = ""; try { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { } return value; } /// /// 读取指定路径和节点的串联值 /// /// 路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 /// 使用示列: /// XMLProsess.Read(path, "/Node", "") /// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']") public static string Read(string path, string node) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { } return value; } /// /// 读取指定路径和节点的属性值 /// /// 路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 /// 使用示列: /// XMLProsess.Read(path, "/Node", "") /// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } /// /// 获取某一节点的所有孩子节点的值 /// /// 要查询的节点 public string[] ReadAllChildallValue(string node) { int i = 0; string[] str = { }; XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点 if (nodelist.Count > 0) { str = new string[nodelist.Count]; foreach (XmlElement el in nodelist)//读元素值 { str[i] = el.Value; i++; } } return str; } /// /// 获取某一节点的所有孩子节点的值 /// /// 要查询的节点 public XmlNodeList ReadAllChild(string node) { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点 return nodelist; } /// /// 读取XML返回经排序或筛选后的DataView /// /// 筛选条件,如:"name='kgdiwss'" /// 排序条件,如:"Id desc" public DataView GetDataViewByXml(string strWhere, string strSort) { try { string XMLFile = this.XMLPath; string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile; DataSet ds = new DataSet(); ds.ReadXml(filename); DataView dv = new DataView(ds.Tables[0]); //创建DataView来完成排序或筛选操作 if (strSort != null) { dv.Sort = strSort; //对DataView中的记录进行排序 } if (strWhere != null) { dv.RowFilter = strWhere; //对DataView中的记录进行筛选,找到我们想要的记录 } return dv; } catch (Exception) { return null; } } #endregion #region 插入数据 /// /// 插入数据 /// /// 路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 属性名,非空时插入该元素属性值,否则插入元素值 /// 值 /// 使用示列: /// XMLProsess.Insert(path, "/Node", "Element", "", "Value") /// XMLProsess.Insert(path, "/Node", "Element", "Attribute", "Value") /// XMLProsess.Insert(path, "/Node", "", "Attribute", "Value") public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + 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(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// /// 插入数据 /// /// 路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 由XML属性名和值组成的二维数组 public static void Insert(string path, string node, string element, string[][] strList) { try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = doc.CreateElement(element); string strAttribute = ""; string strValue = ""; for (int i = 0; i < strList.Length; i++) { for (int j = 0; j < strList[i].Length; j++) { if (j == 0) strAttribute = strList[i][j]; else strValue = strList[i][j]; } if (strAttribute.Equals("")) xe.InnerText = strValue; else xe.SetAttribute(strAttribute, strValue); } xn.AppendChild(xe); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } #endregion #region 修改数据 /// /// 修改指定节点的数据 /// /// 节点 /// 值 public void Update(string node, string value) { try { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLPath); } catch { } } /// /// 修改指定节点的数据 /// /// 路径 /// 节点 /// 值 /// 使用示列: /// XMLProsess.Insert(path, "/Node","Value") /// XMLProsess.Insert(path, "/Node","Value") public static void Update(string path, string node, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// /// 修改指定节点的属性值(静态) /// /// 路径 /// 节点 /// 属性名,非空时修改该节点属性值,否则修改节点值 /// 值 /// 使用示列: /// XMLProsess.Insert(path, "/Node", "", "Value") /// XMLProsess.Insert(path, "/Node", "Attribute", "Value") public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } #endregion #region 删除数据 /// /// 删除节点值 /// /// 路径 /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 /// 值 /// 使用示列: /// XMLProsess.Delete(path, "/Node", "") /// XMLProsess.Delete(path, "/Node", "Attribute") public static void Delete(string path, string node) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); xn.ParentNode.RemoveChild(xn); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// /// 删除数据 /// /// 路径 /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 /// 值 /// 使用示列: /// XMLProsess.Delete(path, "/Node", "") /// XMLProsess.Delete(path, "/Node", "Attribute") public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } #endregion } }