乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#XmlHelper操作Xml文檔的幫助類(lèi)

       javenpop 2015-10-28
      using System.Xml;
      using System.Data;
       
      namespace DotNet.Utilities
      {
          /// <summary>
          /// Xml的操作公共類(lèi)
          /// </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對(duì)象
              /// </summary>
              /// <param name="xmlFilePath">Xml文件的相對(duì)路徑</param>
              public XmlHelper(string xmlFilePath)
              {
                  //獲取XML文件的絕對(duì)路徑
                  _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對(duì)象
                  _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)對(duì)象
              /// <summary>
              /// 獲取指定XPath表達(dá)式的節(jié)點(diǎn)對(duì)象
              /// </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='崗位編號(hào)']",@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='崗位編號(hào)']",@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='崗位編號(hào)']",@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對(duì)象
                  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='崗位編號(hào)']",@itemName是Item節(jié)點(diǎn)的屬性.
              /// </param>
              public void RemoveNode(string xPath)
              {
                  //創(chuàng)建XML的根節(jié)點(diǎn)
                  CreateXMLElement();
       
                  //獲取要?jiǎng)h除的節(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)對(duì)象
              /// <summary>
              /// 創(chuàng)建根節(jié)點(diǎn)對(duì)象
              /// </summary>
              /// <param name="xmlFilePath">Xml文件的相對(duì)路徑</param>        
              private static XmlElement CreateRootElement(string xmlFilePath)
              {
                  //定義變量,表示XML文件的絕對(duì)路徑
                  string filePath = "";
       
                  //獲取XML文件的絕對(duì)路徑
                  filePath = SysHelper.GetPath(xmlFilePath);
       
                  //創(chuàng)建XmlDocument對(duì)象
                  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文件的相對(duì)路徑</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='崗位編號(hào)']",@itemName是Item節(jié)點(diǎn)的屬性.
              /// </param>
              public static string GetValue(string xmlFilePath, string xPath)
              {
                  //創(chuàng)建根對(duì)象
                  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文件的相對(duì)路徑</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='崗位編號(hào)']",@itemName是Item節(jié)點(diǎn)的屬性.
              /// </param>
              /// <param name="attributeName">屬性名</param>
              public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName)
              {
                  //創(chuàng)建根對(duì)象
                  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";//開(kāi)始修改
                  //}
                  //xelem.Save(path);
              }
          }
      }

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

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多