123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- /*******************************************************************************
- * iNethinkCMS - 网站内容管理系统
- * Copyright (C) 2012-2013 inethink.com
- *
- * @author jackyang <69991000@qq.com>
- * @website http://cms.inethink.com
- * @version 1.3.6.0 (2013-08-14)
- *
- * This is licensed under the GNU LGPL, version 3.0 or later.
- * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- namespace iNethinkCMS.Helper
- {
- public class XMLHelper
- {
- public XMLHelper()
- {
- }
- #region XML文档节点查询和读取
- /// <summary>
- /// 选择匹配XPath表达式的第一个节点XmlNode.
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
- /// <returns>返回XmlNode</returns>
- public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)
- {
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- return xmlNode;
- }
- catch (Exception ex)
- {
- return null;
- throw ex; //这里可以定义你自己的异常处理
- }
- }
- /// <summary>
- /// 选择匹配XPath表达式的节点列表XmlNodeList.
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
- /// <returns>返回XmlNodeList</returns>
- public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
- {
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);
- return xmlNodeList;
- }
- catch (Exception ex)
- {
- return null;
- throw ex; //这里可以定义你自己的异常处理
- }
- }
- /// <summary>
- /// 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute.
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
- /// <returns>返回xmlAttributeName</returns>
- public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)
- {
- string content = string.Empty;
- XmlDocument xmlDoc = new XmlDocument();
- XmlAttribute xmlAttribute = null;
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- if (xmlNode != null)
- {
- if (xmlNode.Attributes.Count > 0)
- {
- xmlAttribute = xmlNode.Attributes[xmlAttributeName];
- }
- }
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return xmlAttribute;
- }
- #endregion
- #region XML文档创建和节点或属性的添加、修改
- /// <summary>
- /// 创建一个XML文档
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="rootNodeName">XML文档根节点名称(须指定一个根节点名称)</param>
- /// <param name="version">XML文档版本号(必须为:"1.0")</param>
- /// <param name="encoding">XML文档编码方式</param>
- /// <param name="standalone">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone)
- {
- bool isSuccess = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);
- XmlNode root = xmlDoc.CreateElement(rootNodeName);
- xmlDoc.AppendChild(xmlDeclaration);
- xmlDoc.AppendChild(root);
- xmlDoc.Save(xmlFileName);
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- /// <summary>
- /// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
- /// <param name="innerText">节点文本值</param>
- /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
- /// <param name="value">属性值</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string xmlAttributeName, string value)
- {
- bool isSuccess = false;
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- if (xmlNode != null)
- {
- //存不存在此节点都创建
- XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
- subElement.InnerXml = innerText;
- //如果属性和值参数都不为空则在此新节点上新增属性
- if (!string.IsNullOrEmpty(xmlAttributeName) && !string.IsNullOrEmpty(value))
- {
- XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
- xmlAttribute.Value = value;
- subElement.Attributes.Append(xmlAttribute);
- }
- xmlNode.AppendChild(subElement);
- }
- xmlDoc.Save(xmlFileName); //保存到XML文档
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- /// <summary>
- /// 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建)
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
- /// <param name="innerText">节点文本值</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText)
- {
- bool isSuccess = false;
- bool isExistsNode = false;//标识节点是否存在
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- if (xmlNode != null)
- {
- //遍历xpath节点下的所有子节点
- foreach (XmlNode node in xmlNode.ChildNodes)
- {
- if (node.Name.ToLower() == xmlNodeName.ToLower())
- {
- //存在此节点则更新
- node.InnerXml = innerText;
- isExistsNode = true;
- break;
- }
- }
- if (!isExistsNode)
- {
- //不存在此节点则创建
- XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
- subElement.InnerXml = innerText;
- xmlNode.AppendChild(subElement);
- }
- }
- xmlDoc.Save(xmlFileName); //保存到XML文档
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- /// <summary>
- /// 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建)
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
- /// <param name="value">属性值</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value)
- {
- bool isSuccess = false;
- bool isExistsAttribute = false;//标识属性是否存在
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- if (xmlNode != null)
- {
- //遍历xpath节点中的所有属性
- foreach (XmlAttribute attribute in xmlNode.Attributes)
- {
- if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
- {
- //节点中存在此属性则更新
- attribute.Value = value;
- isExistsAttribute = true;
- break;
- }
- }
- if (!isExistsAttribute)
- {
- //节点中不存在此属性则创建
- XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
- xmlAttribute.Value = value;
- xmlNode.Attributes.Append(xmlAttribute);
- }
- }
- xmlDoc.Save(xmlFileName); //保存到XML文档
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- #endregion
- #region XML文档节点或属性的删除
- /// <summary>
- /// 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除)
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
- {
- bool isSuccess = false;
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- if (xmlNode != null)
- {
- //删除节点
- xmlNode.ParentNode.RemoveChild(xmlNode);
- }
- xmlDoc.Save(xmlFileName); //保存到XML文档
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- /// <summary>
- /// 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <param name="xmlAttributeName">要删除的xmlAttributeName的属性名称</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
- {
- bool isSuccess = false;
- bool isExistsAttribute = false;
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- XmlAttribute xmlAttribute = null;
- if (xmlNode != null)
- {
- //遍历xpath节点中的所有属性
- foreach (XmlAttribute attribute in xmlNode.Attributes)
- {
- if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
- {
- //节点中存在此属性
- xmlAttribute = attribute;
- isExistsAttribute = true;
- break;
- }
- }
- if (isExistsAttribute)
- {
- //删除节点中的属性
- xmlNode.Attributes.Remove(xmlAttribute);
- }
- }
- xmlDoc.Save(xmlFileName); //保存到XML文档
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- /// <summary>
- /// 删除匹配XPath表达式的第一个节点中的所有属性
- /// </summary>
- /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
- /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
- {
- bool isSuccess = false;
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(xmlFileName); //加载XML文档
- XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
- if (xmlNode != null)
- {
- //遍历xpath节点中的所有属性
- xmlNode.Attributes.RemoveAll();
- }
- xmlDoc.Save(xmlFileName); //保存到XML文档
- isSuccess = true;
- }
- catch (Exception ex)
- {
- throw ex; //这里可以定义你自己的异常处理
- }
- return isSuccess;
- }
- #endregion
- }
- }
|