C# XML操作类分享
时间:2021-01-05 13:48:17|栏目:.NET代码|点击: 次
本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
XmlHelper
using System.Xml; using System.Data; namespace DotNet.Utilities { /// <summary> /// Xml的操作公共类 /// </summary> public class XmlHelper { #region 字段定义 /// <summary> /// XML文件的物理路径 /// </summary> private string _filePath = string.Empty; /// <summary> /// Xml文档 /// </summary> private XmlDocument _xml; /// <summary> /// XML的根节点 /// </summary> private XmlElement _element; #endregion #region 构造方法 /// <summary> /// 实例化XmlHelper对象 /// </summary> /// <param name="xmlFilePath">Xml文件的相对路径</param> public XmlHelper(string xmlFilePath) { //获取XML文件的绝对路径 _filePath = SysHelper.GetPath(xmlFilePath); } #endregion #region 创建XML的根节点 /// <summary> /// 创建XML的根节点 /// </summary> private void CreateXMLElement() { //创建一个XML对象 _xml = new XmlDocument(); if (DirFile.IsExistFile(_filePath)) { //加载XML文件 _xml.Load(this._filePath); } //为XML的根节点赋值 _element = _xml.DocumentElement; } #endregion #region 获取指定XPath表达式的节点对象 /// <summary> /// 获取指定XPath表达式的节点对象 /// </summary> /// <param name="xPath">XPath表达式, /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem" /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性. /// </param> public XmlNode GetNode(string xPath) { //创建XML的根节点 CreateXMLElement(); //返回XPath节点 return _element.SelectSingleNode(xPath); } #endregion #region 获取指定XPath表达式节点的值 /// <summary> /// 获取指定XPath表达式节点的值 /// </summary> /// <param name="xPath">XPath表达式, /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem" /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性. /// </param> public string GetValue(string xPath) { //创建XML的根节点 CreateXMLElement(); //返回XPath节点的值 return _element.SelectSingleNode(xPath).InnerText; } #endregion #region 获取指定XPath表达式节点的属性值 /// <summary> /// 获取指定XPath表达式节点的属性值 /// </summary> /// <param name="xPath">XPath表达式, /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem" /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性. /// </param> /// <param name="attributeName">属性名</param> public string GetAttributeValue(string xPath, string attributeName) { //创建XML的根节点 CreateXMLElement(); //返回XPath节点的属性值 return _element.SelectSingleNode(xPath).Attributes[attributeName].Value; } #endregion #region 新增节点 /// <summary> /// 1. 功能:新增节点。 /// 2. 使用条件:将任意节点插入到当前Xml文件中。 /// </summary> /// <param name="xmlNode">要插入的Xml节点</param> public void AppendNode(XmlNode xmlNode) { //创建XML的根节点 CreateXMLElement(); //导入节点 XmlNode node = _xml.ImportNode(xmlNode, true); //将节点插入到根节点下 _element.AppendChild(node); } /// <summary> /// 1. 功能:新增节点。 /// 2. 使用条件:将DataSet中的第一条记录插入Xml文件中。 /// </summary> /// <param name="ds">DataSet的实例,该DataSet中应该只有一条记录</param> public void AppendNode(DataSet ds) { //创建XmlDataDocument对象 XmlDataDocument xmlDataDocument = new XmlDataDocument(ds); //导入节点 XmlNode node = xmlDataDocument.DocumentElement.FirstChild; //将节点插入到根节点下 AppendNode(node); } #endregion #region 删除节点 /// <summary> /// 删除指定XPath表达式的节点 /// </summary> /// <param name="xPath">XPath表达式, /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem" /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性. /// </param> public void RemoveNode(string xPath) { //创建XML的根节点 CreateXMLElement(); //获取要删除的节点 XmlNode node = _xml.SelectSingleNode(xPath); //删除节点 _element.RemoveChild(node); } #endregion //删除节点 #region 保存XML文件 /// <summary> /// 保存XML文件 /// </summary> public void Save() { //创建XML的根节点 CreateXMLElement(); //保存XML文件 _xml.Save(this._filePath); } #endregion //保存XML文件 #region 静态方法 #region 创建根节点对象 /// <summary> /// 创建根节点对象 /// </summary> /// <param name="xmlFilePath">Xml文件的相对路径</param> private static XmlElement CreateRootElement(string xmlFilePath) { //定义变量,表示XML文件的绝对路径 string filePath = ""; //获取XML文件的绝对路径 filePath = SysHelper.GetPath(xmlFilePath); //创建XmlDocument对象 XmlDocument xmlDocument = new XmlDocument(); //加载XML文件 xmlDocument.Load(filePath); //返回根节点 return xmlDocument.DocumentElement; } #endregion #region 获取指定XPath表达式节点的值 /// <summary> /// 获取指定XPath表达式节点的值 /// </summary> /// <param name="xmlFilePath">Xml文件的相对路径</param> /// <param name="xPath">XPath表达式, /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem" /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性. /// </param> public static string GetValue(string xmlFilePath, string xPath) { //创建根对象 XmlElement rootElement = CreateRootElement(xmlFilePath); //返回XPath节点的值 return rootElement.SelectSingleNode(xPath).InnerText; } #endregion #region 获取指定XPath表达式节点的属性值 /// <summary> /// 获取指定XPath表达式节点的属性值 /// </summary> /// <param name="xmlFilePath">Xml文件的相对路径</param> /// <param name="xPath">XPath表达式, /// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem" /// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点. /// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性. /// </param> /// <param name="attributeName">属性名</param> public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName) { //创建根对象 XmlElement rootElement = CreateRootElement(xmlFilePath); //返回XPath节点的属性值 return rootElement.SelectSingleNode(xPath).Attributes[attributeName].Value; } #endregion #endregion public static void SetValue(string xmlFilePath, string xPath, string newtext) { //string path = SysHelper.GetPath(xmlFilePath); //var queryXML = from xmlLog in xelem.Descendants("msg_log") // //所有名字为Bin的记录 // where xmlLog.Element("user").Value == "Bin" // select xmlLog; //foreach (XElement el in queryXML) //{ // el.Element("user").Value = "LiuBin";//开始修改 //} //xelem.Save(path); } } }
XmlProcess
using System; using System.Data; using System.IO; using System.Xml; namespace DotNet.Utilities { public class XMLProcess { #region 构造函数 public XMLProcess() { } public XMLProcess(string strPath) { this._XMLPath = strPath; } #endregion #region 公有属性 private string _XMLPath; public string XMLPath { get { return this._XMLPath; } } #endregion #region 私有方法 /// <summary> /// 导入XML文件 /// </summary> /// <param name="XMLPath">XML文件路径</param> 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 (Exception e) { } return xmldoc; } /// <summary> /// 导入XML文件 /// </summary> /// <param name="XMLPath">XML文件路径</param> 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 (Exception e) { } return xmldoc; } /// <summary> /// 返回完整路径 /// </summary> /// <param name="strPath">Xml的路径</param> private static string GetXmlFullPath(string strPath) { if (strPath.IndexOf(":") > 0) { return strPath; } else { return System.Web.HttpContext.Current.Server.MapPath(strPath); } } #endregion #region 读取数据 /// <summary> /// 读取指定节点的数据 /// </summary> /// <param name="node">节点</param> /// 使用示列: /// 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; } /// <summary> /// 读取指定路径和节点的串联值 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// 使用示列: /// 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; } /// <summary> /// 读取指定路径和节点的属性值 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// 使用示列: /// 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; } /// <summary> /// 获取某一节点的所有孩子节点的值 /// </summary> /// <param name="node">要查询的节点</param> 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; } /// <summary> /// 获取某一节点的所有孩子节点的值 /// </summary> /// <param name="node">要查询的节点</param> public XmlNodeList ReadAllChild(string node) { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点 return nodelist; } /// <summary> /// 读取XML返回经排序或筛选后的DataView /// </summary> /// <param name="strWhere">筛选条件,如:"name='kgdiwss'"</param> /// <param name="strSort"> 排序条件,如:"Id desc"</param> 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; } } /// <summary> /// 读取XML返回DataSet /// </summary> /// <param name="strXmlPath">XML文件相对路径</param> public DataSet GetDataSetByXml(string strXmlPath) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables.Count > 0) { return ds; } return null; } catch (Exception) { return null; } } #endregion #region 插入数据 /// <summary> /// 插入数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param> /// <param name="value">值</param> /// 使用示列: /// 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 { } } /// <summary> /// 插入数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="strList">由XML属性名和值组成的二维数组</param> 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 { } } /// <summary> /// 插入一行数据 /// </summary> /// <param name="strXmlPath">XML文件相对路径</param> /// <param name="Columns">要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};</param> /// <param name="ColumnValue">要插入行每列的值数组,如:string[] ColumnValue={"XML大全","false"};</param> /// <returns>成功返回true,否则返回false</returns> public static bool WriteXmlByDataSet(string strXmlPath, string[] Columns, string[] ColumnValue) { try { //根据传入的XML路径得到.XSD的路径,两个文件放在同一个目录下 string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd"; DataSet ds = new DataSet(); ds.ReadXmlSchema(GetXmlFullPath(strXsdPath)); //读XML架构,关系到列的数据类型 ds.ReadXml(GetXmlFullPath(strXmlPath)); DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); //在原来的表格基础上创建新行 for (int i = 0; i < Columns.Length; i++) //循环给一行中的各个列赋值 { newRow[Columns[i]] = ColumnValue[i]; } dt.Rows.Add(newRow); dt.AcceptChanges(); ds.AcceptChanges(); ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } #endregion #region 修改数据 /// <summary> /// 修改指定节点的数据 /// </summary> /// <param name="node">节点</param> /// <param name="value">值</param> 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 { } } /// <summary> /// 修改指定节点的数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="value">值</param> /// 使用示列: /// 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 { } } /// <summary> /// 修改指定节点的属性值(静态) /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param> /// <param name="value">值</param> /// 使用示列: /// 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 { } } /// <summary> /// 更改符合条件的一条记录 /// </summary> /// <param name="strXmlPath">XML文件路径</param> /// <param name="Columns">列名数组</param> /// <param name="ColumnValue">列值数组</param> /// <param name="strWhereColumnName">条件列名</param> /// <param name="strWhereColumnValue">条件列值</param> public static bool UpdateXmlRow(string strXmlPath, string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue) { try { string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd"; DataSet ds = new DataSet(); ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));//读XML架构,关系到列的数据类型 ds.ReadXml(GetXmlFullPath(strXmlPath)); //先判断行数 if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { //如果当前记录为符合Where条件的记录 if (ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue)) { //循环给找到行的各列赋新值 for (int j = 0; j < Columns.Length; j++) { ds.Tables[0].Rows[i][Columns[j]] = ColumnValue[j]; } ds.AcceptChanges(); //更新DataSet ds.WriteXml(GetXmlFullPath(strXmlPath));//重新写入XML文件 return true; } } } return false; } catch (Exception) { return false; } } #endregion #region 删除数据 /// <summary> /// 删除节点值 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param> /// <param name="value">值</param> /// 使用示列: /// 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 { } } /// <summary> /// 删除数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param> /// <param name="value">值</param> /// 使用示列: /// 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 { } } /// <summary> /// 删除所有行 /// </summary> /// <param name="strXmlPath">XML路径</param> public static bool DeleteXmlAllRows(string strXmlPath) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables[0].Rows.Count > 0) { ds.Tables[0].Rows.Clear(); } ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } /// <summary> /// 通过删除DataSet中指定索引行,重写XML以实现删除指定行 /// </summary> /// <param name="iDeleteRow">要删除的行在DataSet中的Index值</param> public static bool DeleteXmlRowByIndex(string strXmlPath, int iDeleteRow) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables[0].Rows.Count > 0) { ds.Tables[0].Rows[iDeleteRow].Delete(); } ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } /// <summary> /// 删除指定列中指定值的行 /// </summary> /// <param name="strXmlPath">XML相对路径</param> /// <param name="strColumn">列名</param> /// <param name="ColumnValue">指定值</param> public static bool DeleteXmlRows(string strXmlPath, string strColumn, string[] ColumnValue) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables[0].Rows.Count > 0) { //判断行多还是删除的值多,多的for循环放在里面 if (ColumnValue.Length > ds.Tables[0].Rows.Count) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ColumnValue.Length; j++) { if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j])) { ds.Tables[0].Rows[i].Delete(); } } } } else { for (int j = 0; j < ColumnValue.Length; j++) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j])) { ds.Tables[0].Rows[i].Delete(); } } } } ds.WriteXml(GetXmlFullPath(strXmlPath)); } return true; } catch (Exception) { return false; } } #endregion } }
上一篇:限制CheckBoxList控件只能单选实现代码及演示动画
栏 目:.NET代码
本文标题:C# XML操作类分享
本文地址:http://www.codeinn.net/misctech/40922.html