日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C#XmlHelper操作Xml文檔的幫助類

 javenpop 2015-10-28
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的根節(jié)點(diǎn)
        /// </summary>
        private XmlElement _element;
        #endregion
 
        #region 構(gòu)造方法
        /// <summary>
        /// 實(shí)例化XmlHelper對象
        /// </summary>
        /// <param name="xmlFilePath">Xml文件的相對路徑</param>
        public XmlHelper(string xmlFilePath)
        {
            //獲取XML文件的絕對路徑
            _filePath = SysHelper.GetPath(xmlFilePath);
        }
        #endregion
 
        #region 創(chuàng)建XML的根節(jié)點(diǎn)
        /// <summary>
        /// 創(chuàng)建XML的根節(jié)點(diǎn)
        /// </summary>
        private void CreateXMLElement()
        {
 
            //創(chuàng)建一個(gè)XML對象
            _xml = new XmlDocument();
 
            if (DirFile.IsExistFile(_filePath))
            {
                //加載XML文件
                _xml.Load(this._filePath);
            }
 
            //為XML的根節(jié)點(diǎn)賦值
            _element = _xml.DocumentElement;
        }
        #endregion
 
        #region 獲取指定XPath表達(dá)式的節(jié)點(diǎn)對象
        /// <summary>
        /// 獲取指定XPath表達(dá)式的節(jié)點(diǎn)對象
        /// </summary>        
        /// <param name="xPath">XPath表達(dá)式,
        /// 范例1: @"Skill/First/SkillItem", 等效于 @"http://Skill/First/SkillItem"
        /// 范例2: @"Table[USERNAME='a']" , []表示篩選,USERNAME是Table下的一個(gè)子節(jié)點(diǎn).
        /// 范例3: @"ApplyPost/Item[@itemName='崗位編號']",@itemName是Item節(jié)點(diǎn)的屬性.
        /// </param>
        public XmlNode GetNode(string xPath)
        {
            //創(chuàng)建XML的根節(jié)點(diǎn)
            CreateXMLElement();
 
            //返回XPath節(jié)點(diǎn)
            return _element.SelectSingleNode(xPath);
        }
        #endregion
 
        #region 獲取指定XPath表達(dá)式節(jié)點(diǎn)的值
        /// <summary>
        /// 獲取指定XPath表達(dá)式節(jié)點(diǎn)的值
        /// </summary>
        /// <param name="xPath">XPath表達(dá)式,
        /// 范例1: @"Skill/First/SkillItem", 等效于 @"http://Skill/First/SkillItem"
        /// 范例2: @"Table[USERNAME='a']" , []表示篩選,USERNAME是Table下的一個(gè)子節(jié)點(diǎn).
        /// 范例3: @"ApplyPost/Item[@itemName='崗位編號']",@itemName是Item節(jié)點(diǎn)的屬性.
        /// </param>
        public string GetValue(string xPath)
        {
            //創(chuàng)建XML的根節(jié)點(diǎn)
            CreateXMLElement();
 
            //返回XPath節(jié)點(diǎn)的值
            return _element.SelectSingleNode(xPath).InnerText;
        }
        #endregion
 
        #region 獲取指定XPath表達(dá)式節(jié)點(diǎn)的屬性值
        /// <summary>
        /// 獲取指定XPath表達(dá)式節(jié)點(diǎn)的屬性值
        /// </summary>
        /// <param name="xPath">XPath表達(dá)式,
        /// 范例1: @"Skill/First/SkillItem", 等效于 @"http://Skill/First/SkillItem"
        /// 范例2: @"Table[USERNAME='a']" , []表示篩選,USERNAME是Table下的一個(gè)子節(jié)點(diǎn).
        /// 范例3: @"ApplyPost/Item[@itemName='崗位編號']",@itemName是Item節(jié)點(diǎn)的屬性.
        /// </param>
        /// <param name="attributeName">屬性名</param>
        public string GetAttributeValue(string xPath, string attributeName)
        {
            //創(chuàng)建XML的根節(jié)點(diǎn)
            CreateXMLElement();
 
            //返回XPath節(jié)點(diǎn)的屬性值
            return _element.SelectSingleNode(xPath).Attributes[attributeName].Value;
        }
        #endregion
 
        #region 新增節(jié)點(diǎn)
        /// <summary>
        /// 1. 功能:新增節(jié)點(diǎn)。
        /// 2. 使用條件:將任意節(jié)點(diǎn)插入到當(dāng)前Xml文件中。
        /// </summary>        
        /// <param name="xmlNode">要插入的Xml節(jié)點(diǎn)</param>
        public void AppendNode(XmlNode xmlNode)
        {
            //創(chuàng)建XML的根節(jié)點(diǎn)
            CreateXMLElement();
 
            //導(dǎo)入節(jié)點(diǎn)
            XmlNode node = _xml.ImportNode(xmlNode, true);
 
            //將節(jié)點(diǎn)插入到根節(jié)點(diǎn)下
            _element.AppendChild(node);
        }
 
        /// <summary>
        /// 1. 功能:新增節(jié)點(diǎn)。
        /// 2. 使用條件:將DataSet中的第一條記錄插入Xml文件中。
        /// </summary>        
        /// <param name="ds">DataSet的實(shí)例,該DataSet中應(yīng)該只有一條記錄</param>
        public void AppendNode(DataSet ds)
        {
            //創(chuàng)建XmlDataDocument對象
            XmlDataDocument xmlDataDocument = new XmlDataDocument(ds);
 
            //導(dǎo)入節(jié)點(diǎn)
            XmlNode node = xmlDataDocument.DocumentElement.FirstChild;
 
            //將節(jié)點(diǎn)插入到根節(jié)點(diǎn)下
            AppendNode(node);
        }
        #endregion
 
        #region 刪除節(jié)點(diǎn)
        /// <summary>
        /// 刪除指定XPath表達(dá)式的節(jié)點(diǎn)
        /// </summary>        
        /// <param name="xPath">XPath表達(dá)式,
        /// 范例1: @"Skill/First/SkillItem", 等效于 @"http://Skill/First/SkillItem"
        /// 范例2: @"Table[USERNAME='a']" , []表示篩選,USERNAME是Table下的一個(gè)子節(jié)點(diǎn).
        /// 范例3: @"ApplyPost/Item[@itemName='崗位編號']",@itemName是Item節(jié)點(diǎn)的屬性.
        /// </param>
        public void RemoveNode(string xPath)
        {
            //創(chuàng)建XML的根節(jié)點(diǎn)
            CreateXMLElement();
 
            //獲取要刪除的節(jié)點(diǎn)
            XmlNode node = _xml.SelectSingleNode(xPath);
 
            //刪除節(jié)點(diǎn)
            _element.RemoveChild(node);
        }
        #endregion //刪除節(jié)點(diǎn)
 
        #region 保存XML文件
        /// <summary>
        /// 保存XML文件
        /// </summary>        
        public void Save()
        {
            //創(chuàng)建XML的根節(jié)點(diǎn)
            CreateXMLElement();
 
            //保存XML文件
            _xml.Save(this._filePath);
        }
        #endregion //保存XML文件
 
        #region 靜態(tài)方法
 
        #region 創(chuàng)建根節(jié)點(diǎn)對象
        /// <summary>
        /// 創(chuàng)建根節(jié)點(diǎn)對象
        /// </summary>
        /// <param name="xmlFilePath">Xml文件的相對路徑</param>        
        private static XmlElement CreateRootElement(string xmlFilePath)
        {
            //定義變量,表示XML文件的絕對路徑
            string filePath = "";
 
            //獲取XML文件的絕對路徑
            filePath = SysHelper.GetPath(xmlFilePath);
 
            //創(chuàng)建XmlDocument對象
            XmlDocument xmlDocument = new XmlDocument();
            //加載XML文件
            xmlDocument.Load(filePath);
 
            //返回根節(jié)點(diǎn)
            return xmlDocument.DocumentElement;
        }
        #endregion
 
        #region 獲取指定XPath表達(dá)式節(jié)點(diǎn)的值
        /// <summary>
        /// 獲取指定XPath表達(dá)式節(jié)點(diǎn)的值
        /// </summary>
        /// <param name="xmlFilePath">Xml文件的相對路徑</param>
        /// <param name="xPath">XPath表達(dá)式,
        /// 范例1: @"Skill/First/SkillItem", 等效于 @"http://Skill/First/SkillItem"
        /// 范例2: @"Table[USERNAME='a']" , []表示篩選,USERNAME是Table下的一個(gè)子節(jié)點(diǎn).
        /// 范例3: @"ApplyPost/Item[@itemName='崗位編號']",@itemName是Item節(jié)點(diǎn)的屬性.
        /// </param>
        public static string GetValue(string xmlFilePath, string xPath)
        {
            //創(chuàng)建根對象
            XmlElement rootElement = CreateRootElement(xmlFilePath);
 
            //返回XPath節(jié)點(diǎn)的值
            return rootElement.SelectSingleNode(xPath).InnerText;
        }
        #endregion
 
        #region 獲取指定XPath表達(dá)式節(jié)點(diǎn)的屬性值
        /// <summary>
        /// 獲取指定XPath表達(dá)式節(jié)點(diǎn)的屬性值
        /// </summary>
        /// <param name="xmlFilePath">Xml文件的相對路徑</param>
        /// <param name="xPath">XPath表達(dá)式,
        /// 范例1: @"Skill/First/SkillItem", 等效于 @"http://Skill/First/SkillItem"
        /// 范例2: @"Table[USERNAME='a']" , []表示篩選,USERNAME是Table下的一個(gè)子節(jié)點(diǎn).
        /// 范例3: @"ApplyPost/Item[@itemName='崗位編號']",@itemName是Item節(jié)點(diǎn)的屬性.
        /// </param>
        /// <param name="attributeName">屬性名</param>
        public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName)
        {
            //創(chuàng)建根對象
            XmlElement rootElement = CreateRootElement(xmlFilePath);
 
            //返回XPath節(jié)點(diǎn)的屬性值
            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);
        }
    }
}

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多