using System;
using System.Xml;
/** <summary>
/// XMLHelper XML文档操作管理器
/// </summary>
public class XMLHelper
{
public XMLHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region XML文档节点查询和读取
/** <summary>
/// 选择匹配XPath表达式的第一个节点XmlNode.
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
/// <returns>返回XmlNode</returns>
public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
return xmlNode;
}
catch (Exception ex)
{
return null;
//throw ex; //这里可以定义你自己的异常处理
}
}
/** <summary>
/// 选择匹配XPath表达式的节点列表XmlNodeList.
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
/// <returns>返回XmlNodeList</returns>
public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);
return xmlNodeList;
}
catch (Exception ex)
{
return null;
//throw ex; //这里可以定义你自己的异常处理
}
}
/** <summary>
/// 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute.
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
/// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
/// <returns>返回xmlAttributeName</returns>
public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)
{
string content = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
XmlAttribute xmlAttribute = null;
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
if (xmlNode.Attribu