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);
}
}
}
|