123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System.Xml;
- using System.Data;
- namespace Ant.Service.Utilities
- {
-
-
-
- public class XmlHelper
- {
- #region 字段定义
-
-
-
- private string _filePath = string.Empty;
-
-
-
- private XmlDocument _xml;
-
-
-
- private XmlElement _element;
- #endregion
- #region 构造方法
-
-
-
-
- public XmlHelper(string xmlFilePath)
- {
-
- _filePath = SysHelper.GetPath(xmlFilePath);
- }
- #endregion
- #region 创建XML的根节点
-
-
-
- private void CreateXMLElement()
- {
-
- _xml = new XmlDocument();
- if (DirFile.IsExistFile(_filePath))
- {
-
- _xml.Load(this._filePath);
- }
-
- _element = _xml.DocumentElement;
- }
- #endregion
- #region 获取指定XPath表达式的节点对象
-
-
-
-
-
-
-
-
- public XmlNode GetNode(string xPath)
- {
-
- CreateXMLElement();
-
- return _element.SelectSingleNode(xPath);
- }
- #endregion
- #region 获取指定XPath表达式节点的值
-
-
-
-
-
-
-
-
- public string GetValue(string xPath)
- {
-
- CreateXMLElement();
-
- return _element.SelectSingleNode(xPath).InnerText;
- }
- #endregion
- #region 获取指定XPath表达式节点的属性值
-
-
-
-
-
-
-
-
-
- public string GetAttributeValue(string xPath, string attributeName)
- {
-
- CreateXMLElement();
-
- return _element.SelectSingleNode(xPath).Attributes[attributeName].Value;
- }
- #endregion
- #region 新增节点
-
-
-
-
-
- public void AppendNode(XmlNode xmlNode)
- {
-
- CreateXMLElement();
-
- XmlNode node = _xml.ImportNode(xmlNode, true);
-
- _element.AppendChild(node);
- }
-
-
-
-
-
- public void AppendNode(DataSet ds)
- {
-
- XmlDataDocument xmlDataDocument = new XmlDataDocument(ds);
-
- XmlNode node = xmlDataDocument.DocumentElement.FirstChild;
-
- AppendNode(node);
- }
- #endregion
- #region 删除节点
-
-
-
-
-
-
-
-
- public void RemoveNode(string xPath)
- {
-
- CreateXMLElement();
-
- XmlNode node = _xml.SelectSingleNode(xPath);
-
- _element.RemoveChild(node);
- }
- #endregion //删除节点
- #region 保存XML文件
-
-
-
- public void Save()
- {
-
- CreateXMLElement();
-
- _xml.Save(this._filePath);
- }
- #endregion //保存XML文件
- #region 静态方法
- #region 创建根节点对象
-
-
-
-
- private static XmlElement CreateRootElement(string xmlFilePath)
- {
-
- string filePath = "";
-
- filePath = SysHelper.GetPath(xmlFilePath);
-
- XmlDocument xmlDocument = new XmlDocument();
-
- xmlDocument.Load(filePath);
-
- return xmlDocument.DocumentElement;
- }
- #endregion
- #region 获取指定XPath表达式节点的值
-
-
-
-
-
-
-
-
-
- public static string GetValue(string xmlFilePath, string xPath)
- {
-
- XmlElement rootElement = CreateRootElement(xmlFilePath);
-
- return rootElement.SelectSingleNode(xPath).InnerText;
- }
- #endregion
- #region 获取指定XPath表达式节点的属性值
-
-
-
-
-
-
-
-
-
-
- public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName)
- {
-
- XmlElement rootElement = CreateRootElement(xmlFilePath);
-
- return rootElement.SelectSingleNode(xPath).Attributes[attributeName].Value;
- }
- #endregion
- #endregion
- public static void SetValue(string xmlFilePath, string xPath, string newtext)
- {
-
-
-
-
-
-
-
-
-
-
- }
- }
- }
|