293 lines
8.3 KiB
C#
293 lines
8.3 KiB
C#
|
|
using System.Xml;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System;
|
|
|
|
namespace MAX.Framework.Helpers
|
|
{
|
|
/// <summary>
|
|
/// Xml的操作公共类
|
|
/// </summary>
|
|
public class XmlHelper
|
|
{
|
|
#region 字段定义
|
|
/// <summary>
|
|
/// /// XML文件的物理路径
|
|
/// </summary>
|
|
private string _filePath = string.Empty;
|
|
/// <summary>
|
|
/// Xml文档
|
|
/// </summary>
|
|
private XmlDocument _xml;
|
|
/// <summary>
|
|
/// XML的根节点
|
|
/// </summary>
|
|
private XmlElement _element;
|
|
#endregion
|
|
|
|
#region 构造方法
|
|
/// <summary>
|
|
/// 实例化XmlHelper对象
|
|
/// </summary>
|
|
/// <param name="xmlFilePath">Xml文件的相对路径</param>
|
|
public XmlHelper(string xmlFilePath)
|
|
{
|
|
//获取XML文件的绝对路径
|
|
_filePath = xmlFilePath;
|
|
//创建一个XML对象
|
|
_xml = new XmlDocument();
|
|
if (File.Exists(xmlFilePath))
|
|
{
|
|
//加载XML文件
|
|
_xml.Load(xmlFilePath);
|
|
}
|
|
_element = _xml.DocumentElement;
|
|
}
|
|
public XmlHelper(Stream stream)
|
|
{
|
|
//创建一个XML对象
|
|
_xml = new XmlDocument();
|
|
if (stream != null)
|
|
{
|
|
//加载XML
|
|
_xml.Load(stream);
|
|
}
|
|
_element = _xml.DocumentElement;
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
#region 获取指定XPath表达式的节点对象
|
|
/// <summary>
|
|
/// 获取指定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)
|
|
{
|
|
//返回XPath节点
|
|
return _element.SelectSingleNode(xPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定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 XmlNodeList GetNodes(string xPath)
|
|
{
|
|
//返回XPath节点
|
|
return _element.SelectNodes(xPath);
|
|
}
|
|
#endregion
|
|
|
|
#region 获取指定XPath表达式节点的值
|
|
/// <summary>
|
|
/// 获取指定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 string GetValue(string xPath)
|
|
{
|
|
//返回XPath节点的值
|
|
string value = _element.SelectSingleNode(xPath)?.InnerText ?? "";
|
|
return value;
|
|
}
|
|
public T GetValue<T>(string xPath, T? def = default)
|
|
{
|
|
string value = GetValue(xPath);
|
|
if (value == null)
|
|
return def;
|
|
else
|
|
return (T)Convert.ChangeType(value, typeof(T));
|
|
}
|
|
#endregion
|
|
|
|
#region 获取指定XPath表达式节点的属性值
|
|
/// <summary>
|
|
/// 获取指定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>
|
|
/// <param name="attributeName">属性名</param>
|
|
public string GetAttributeValue(string xPath, string attributeName)
|
|
{
|
|
//返回XPath节点的属性值
|
|
return _element.SelectSingleNode(xPath).Attributes[attributeName].Value;
|
|
}
|
|
#endregion
|
|
|
|
#region 新增节点
|
|
/// <summary>
|
|
/// 1. 功能:新增节点。
|
|
/// 2. 使用条件:将任意节点插入到当前Xml文件中。
|
|
/// </summary>
|
|
/// <param name="xmlNode">要插入的Xml节点</param>
|
|
public void AppendNode(XmlNode xmlNode)
|
|
{
|
|
//导入节点
|
|
XmlNode node = _xml.ImportNode(xmlNode, true);
|
|
|
|
//将节点插入到根节点下
|
|
_element.AppendChild(node);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 1. 功能:新增节点。
|
|
/// 2. 使用条件:将DataSet中的第一条记录插入Xml文件中。
|
|
/// </summary>
|
|
/// <param name="ds">DataSet的实例,该DataSet中应该只有一条记录</param>
|
|
public void AppendNode(DataSet ds)
|
|
{
|
|
//创建XmlDataDocument对象
|
|
XmlDataDocument xmlDataDocument = new XmlDataDocument(ds);
|
|
|
|
//导入节点
|
|
XmlNode node = xmlDataDocument.DocumentElement.FirstChild;
|
|
|
|
//将节点插入到根节点下
|
|
AppendNode(node);
|
|
}
|
|
#endregion
|
|
|
|
#region 删除节点
|
|
/// <summary>
|
|
/// 删除指定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 void RemoveNode(string xPath)
|
|
{
|
|
//获取要删除的节点
|
|
XmlNode node = _xml.SelectSingleNode(xPath);
|
|
|
|
//删除节点
|
|
_element.RemoveChild(node);
|
|
}
|
|
#endregion //删除节点
|
|
|
|
#region 保存XML文件
|
|
/// <summary>
|
|
/// 保存XML文件
|
|
/// </summary>
|
|
public void Save()
|
|
{
|
|
if (string.IsNullOrEmpty(_filePath))
|
|
{
|
|
throw new Exception("Xml文件路径不能为空!");
|
|
}
|
|
//保存XML文件
|
|
_xml.Save(_filePath);
|
|
}
|
|
public void Save(string filePath)
|
|
{
|
|
//保存XML文件
|
|
_xml.Save(filePath);
|
|
}
|
|
#endregion //保存XML文件
|
|
|
|
#region 静态方法
|
|
|
|
#region 创建根节点对象
|
|
/// <summary>
|
|
/// 创建根节点对象
|
|
/// </summary>
|
|
/// <param name="xmlFilePath">Xml文件的相对路径</param>
|
|
private static XmlElement CreateRootElement(string xmlFilePath)
|
|
{
|
|
//定义变量,表示XML文件的绝对路径
|
|
string filePath = "";
|
|
|
|
//获取XML文件的绝对路径
|
|
filePath = xmlFilePath;
|
|
|
|
//创建XmlDocument对象
|
|
XmlDocument xmlDocument = new XmlDocument();
|
|
//加载XML文件
|
|
xmlDocument.Load(filePath);
|
|
|
|
//返回根节点
|
|
return xmlDocument.DocumentElement;
|
|
}
|
|
#endregion
|
|
|
|
#region 获取指定XPath表达式节点的值
|
|
/// <summary>
|
|
/// 获取指定XPath表达式节点的值
|
|
/// </summary>
|
|
/// <param name="xmlFilePath">Xml文件的相对路径</param>
|
|
/// <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 static string GetValue(string xmlFilePath, string xPath)
|
|
{
|
|
//创建根对象
|
|
XmlElement rootElement = CreateRootElement(xmlFilePath);
|
|
|
|
//返回XPath节点的值
|
|
return rootElement.SelectSingleNode(xPath).InnerText;
|
|
}
|
|
#endregion
|
|
|
|
#region 获取指定XPath表达式节点的属性值
|
|
/// <summary>
|
|
/// 获取指定XPath表达式节点的属性值
|
|
/// </summary>
|
|
/// <param name="xmlFilePath">Xml文件的相对路径</param>
|
|
/// <param name="xPath">XPath表达式,
|
|
/// 范例1: @"Skill/First/SkillItem", 等效于 @"//Skill/First/SkillItem"
|
|
/// 范例2: @"Table[USERNAME='a']" , []表示筛选,USERNAME是Table下的一个子节点.
|
|
/// 范例3: @"ApplyPost/Item[@itemName='岗位编号']",@itemName是Item节点的属性.
|
|
/// </param>
|
|
/// <param name="attributeName">属性名</param>
|
|
public static string GetAttributeValue(string xmlFilePath, string xPath, string attributeName)
|
|
{
|
|
//创建根对象
|
|
XmlElement rootElement = CreateRootElement(xmlFilePath);
|
|
|
|
//返回XPath节点的属性值
|
|
return rootElement.SelectSingleNode(xPath).Attributes[attributeName].Value;
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
public static class XmlHelExtension
|
|
{
|
|
public static string GetAttributeValue(this XmlNode node, string attributeName)
|
|
{
|
|
return node.Attributes[attributeName].Value;
|
|
}
|
|
public static T GetAttributeValue<T>(this XmlNode node, string attributeName)
|
|
{
|
|
var value = node.Attributes[attributeName].Value;
|
|
if (string.IsNullOrEmpty(value))
|
|
return default(T);
|
|
return (T)Convert.ChangeType(value, typeof(T));
|
|
|
|
}
|
|
}
|
|
}
|