using System.Xml;
using System.Data;
namespace DotNet.Utilities
{
/// <summary>
/// Xml的操作公共類
/// </summary>
public class XmlHelper
{
#region 字段定義
/// <summary>
/// XML檔案的實體路徑
/// </summary>
private string _filePath = string.Empty;
/// Xml文檔
private XmlDocument _xml;
/// XML的根節點
private XmlElement _element;
#endregion
#region 構造方法
/// 執行個體化XmlHelper對象
/// <param name="xmlFilePath">Xml檔案的相對路徑</param>
public XmlHelper(string xmlFilePath)
{
//擷取XML檔案的絕對路徑
_filePath = SysHelper.GetPath(xmlFilePath);
}
#region 建立XML的根節點
/// 建立XML的根節點
private void CreateXMLElement()
//建立一個XML對象
_xml = new XmlDocument();
if (DirFile.IsExistFile(_filePath))
{
//加載XML檔案
_xml.Load(this._filePath);
}
//為XML的根節點指派
_element = _xml.DocumentElement;
#region 擷取指定XPath表達式的節點對象
/// 擷取指定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);
#region 擷取指定XPath表達式節點的值
/// 擷取指定XPath表達式節點的值
public string GetValue(string xPath)
//傳回XPath節點的值
return _element.SelectSingleNode(xPath).InnerText;
#region 擷取指定XPath表達式節點的屬性值
/// 擷取指定XPath表達式節點的屬性值
/// <param name="attributeName">屬性名</param>
public string GetAttributeValue(string xPath, string attributeName)
//傳回XPath節點的屬性值
return _element.SelectSingleNode(xPath).Attributes[attributeName].Value;
#region 新增節點
/// 1. 功能:新增節點。
/// 2. 使用條件:将任意節點插入到目前Xml檔案中。
/// <param name="xmlNode">要插入的Xml節點</param>
public void AppendNode(XmlNode xmlNode)
//導入節點
XmlNode node = _xml.ImportNode(xmlNode, true);
//将節點插入到根節點下
_element.AppendChild(node);
/// 2. 使用條件:将DataSet中的第一條記錄插入Xml檔案中。
/// <param name="ds">DataSet的執行個體,該DataSet中應該隻有一條記錄</param>
public void AppendNode(DataSet ds)
//建立XmlDataDocument對象
XmlDataDocument xmlDataDocument = new XmlDataDocument(ds);
XmlNode node = xmlDataDocument.DocumentElement.FirstChild;
AppendNode(node);
#region 删除節點
/// 删除指定XPath表達式的節點
public void RemoveNode(string xPath)
//擷取要删除的節點
XmlNode node = _xml.SelectSingleNode(xPath);
//删除節點
_element.RemoveChild(node);
#endregion //删除節點
#region 儲存XML檔案
/// 儲存XML檔案
public void Save()
//儲存XML檔案
_xml.Save(this._filePath);
#endregion //儲存XML檔案
#region 靜态方法
#region 建立根節點對象
/// 建立根節點對象
/// <param name="xmlFilePath">Xml檔案的相對路徑</param>
private static XmlElement CreateRootElement(string xmlFilePath)
//定義變量,表示XML檔案的絕對路徑
string filePath = "";
filePath = SysHelper.GetPath(xmlFilePath);
//建立XmlDocument對象
XmlDocument xmlDocument = new XmlDocument();
//加載XML檔案
xmlDocument.Load(filePath);
//傳回根節點
return xmlDocument.DocumentElement;
public static string GetValue(string xmlFilePath, string xPath)
//建立根對象
XmlElement rootElement = CreateRootElement(xmlFilePath);
return rootElement.SelectSingleNode(xPath).InnerText;
public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName)
return rootElement.SelectSingleNode(xPath).Attributes[attributeName].Value;
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);
}
}