C# XML操作封装
利用XPath操作XML文档。
//下面列出了最有用的路径表达式:
// nodename 选取此节点的所有子节点。
// / 从根节点选取。
// // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
// . 选取当前节点。
// .. 选取当前节点的父节点。
// @ 选取属性
///<summary>
/// xml解析工具
///</summary>
public class XmlTool
{
#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
{
//加载XML文档
xmlDoc.Load(xmlFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
return xmlNode;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数GetXmlNodeByXpath,总参数2,错误信息->" + ex.Message);
log.Error("函数GetXmlNodeByXpath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数GetXmlNodeByXpath,参数2,xpath->" + xpath);
return null;
}
}
///<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)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数GetXmlNodeListByXpath,总参数2,错误信息->" + ex.Message);
log.Error("函数GetXmlNodeListByXpath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数GetXmlNodeListByXpath,参数2,xpath->" + xpath);
return null;
}
}
///<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)
{
XmlDocument xmlDoc = new XmlDocument();
XmlAttribute xmlAttribute = null;
try
{
//加载XML文档
xmlDoc.Load(xmlFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
if (xmlNode.Attributes.Count > 0)
{
xmlAttribute = xmlNode.Attributes[xmlAttributeName];
}
}
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数GetXmlAttribute,总参数3,错误信息->" + ex.Message);
log.Error("函数GetXmlAttribute,参数1,xmlFileName->" + xmlFileName);
log.Error("函数GetXmlAttribute,参数2,xpath->" + xpath);
log.Error("函数GetXmlAttribute,参数3,xmlAttributeName->" + xmlAttributeName);
return null;
}
return xmlAttribute;
}
public static XmlAttribute GetSpecialAttributeValue(string xmlFileName, string attributeName)
{
XmlDocument xmlDoc = new XmlDocument();
XmlAttribute xmlAttribute = null;
try
{
//加载XML文档
xmlDoc.Load(xmlFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode("/");
if (xmlNode != null)
{
XmlNode node = xmlNode.LastChild;
if (node.Attributes.Count > 0)
{
xmlAttribute = node.Attributes[attributeName];
}
}
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数GetSpecialAttributeValue,总参数2,错误信息->" + ex.Message);
log.Error("函数GetSpecialAttributeValue,参数1,xmlFileName->" + xmlFileName);
log.Error("函数GetSpecialAttributeValue,参数2,attributeName->" + attributeName);
return null;
}
return xmlAttribute;
}
#endregion
#region XML文档创建和节点或属性的添加、修改
///<summary>
/// 创建一个XML文档
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="rootNodeName">XML文档根节点名称(须指定一个根节点名称)</param>
///<param name="version">XML文档版本号(必须为:"1.0")</param>
///<param name="encoding">XML文档编码方式</param>
///<param name="standalone">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</param>
///<returns>成功返回true,失败返回false</returns>
public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone)
{
// 文件路径,版本号,根节点名不能为空
if (string.IsNullOrEmpty(xmlFileName) ||
string.IsNullOrEmpty(rootNodeName) ||
string.IsNullOrEmpty(version))
{
return false;
}
bool isSuccess = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);
XmlNode root = xmlDoc.CreateElement(rootNodeName);
xmlDoc.AppendChild(xmlDeclaration);
xmlDoc.AppendChild(root);
xmlDoc.Save(xmlFileName);
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数CreateXmlDocument,总参数5,错误信息->" + ex.Message);
log.Error("函数CreateXmlDocument,参数1,xmlFileName->" + xmlFileName);
log.Error("函数CreateXmlDocument,参数2,rootNodeName->" + rootNodeName);
log.Error("函数CreateXmlDocument,参数3,version->" + version);
log.Error("函数CreateXmlDocument,参数4,encoding->" + encoding);
log.Error("函数CreateXmlDocument,参数5,standalone->" + standalone);
}
return isSuccess;
}
/// <summary>
/// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点)
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
/// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
/// <param name="innerText">节点文本值</param>
/// <param name="listAttribute">要添加的xmlAttributeName的属性</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, List<KeyValuePair<string, string>> listAttribute)
{
if(string.IsNullOrEmpty(xmlFileName) ||
string.IsNullOrEmpty(xpath) ||
string.IsNullOrEmpty(xmlNodeName))
{
return false;
}
bool isSuccess = false;
XmlDocument xmlDoc = new XmlDocument();
try
{
//加载XML文档
xmlDoc.Load(xmlFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
//存不存在此节点都创建
XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
subElement.InnerText = (innerText == null ? "" : innerText);
//如果属性和值参数都不为空则在此新节点上新增属性
if (null != listAttribute)
{
foreach (KeyValuePair<string, string> xmlAttr in listAttribute)
{
if (!string.IsNullOrEmpty(xmlAttr.Key) && !string.IsNullOrEmpty(xmlAttr.Value))
{
XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttr.Key);
xmlAttribute.Value = xmlAttr.Value;
subElement.Attributes.Append(xmlAttribute);
}
}
}
xmlNode.AppendChild(subElement);
}
//保存到XML文档
xmlDoc.Save(xmlFileName);
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数CreateXmlNodeByXPath,总参数5,错误信息->" + ex.Message);
log.Error("函数CreateXmlNodeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数CreateXmlNodeByXPath,参数2,xpath->" + xpath);
log.Error("函数CreateXmlNodeByXPath,参数3,xmlNodeName->" + xmlNodeName);
log.Error("函数CreateXmlNodeByXPath,参数4,innerText->" + innerText);
log.Error("函数CreateXmlNodeByXPath,参数5,listAttribute为list");
}
return isSuccess;
}
///<summary>
/// 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建)
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
///<param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
///<param name="innerText">节点文本值</param>
///<returns>成功返回true,失败返回false</returns>
public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText)
{
if (string.IsNullOrEmpty(xmlFileName) ||
string.IsNullOrEmpty(xpath) ||
string.IsNullOrEmpty(xmlNodeName))
{
return false;
}
bool isSuccess = false;
//标识节点是否存在
bool isExistsNode = false;
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
//遍历xpath节点下的所有子节点
foreach (XmlNode node in xmlNode.ChildNodes)
{
if (node.Name.ToLower() == xmlNodeName.ToLower())
{
//存在此节点则更新
node.InnerText = (innerText == null ? "" : innerText);
isExistsNode = true;
break;
}
}
if (!isExistsNode)
{
//不存在此节点则创建
XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
subElement.InnerXml = innerText;
xmlNode.AppendChild(subElement);
}
}
xmlDoc.Save(xmlFileName); //保存到XML文档
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数CreateXmlNodeByXPath,总参数4,错误信息->" + ex.Message);
log.Error("函数CreateXmlNodeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数CreateXmlNodeByXPath,参数2,xpath->" + xpath);
log.Error("函数CreateXmlNodeByXPath,参数3,xmlNodeName->" + xmlNodeName);
log.Error("函数CreateXmlNodeByXPath,参数4,innerText->" + innerText);
}
return isSuccess;
}
///<summary>
/// 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建)
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
///<param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
///<param name="value">属性值</param>
///<returns>成功返回true,失败返回false</returns>
public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value)
{
if (string.IsNullOrEmpty(xmlFileName) ||
string.IsNullOrEmpty(xpath) ||
string.IsNullOrEmpty(xmlAttributeName))
{
return false;
}
bool isSuccess = false;
bool isExistsAttribute = false;//标识属性是否存在
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
//遍历xpath节点中的所有属性
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
{
//节点中存在此属性则更新
attribute.Value = value;
isExistsAttribute = true;
break;
}
}
if (!isExistsAttribute)
{
//节点中不存在此属性则创建
XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
xmlAttribute.Value = value;
xmlNode.Attributes.Append(xmlAttribute);
}
}
xmlDoc.Save(xmlFileName); //保存到XML文档
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数CreateOrUpdateXmlAttributeByXPath,总参数4,错误信息->" + ex.Message);
log.Error("函数CreateOrUpdateXmlAttributeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数CreateOrUpdateXmlAttributeByXPath,参数2,xpath->" + xpath);
log.Error("函数CreateOrUpdateXmlAttributeByXPath,参数3,xmlAttributeName->" + xmlAttributeName);
log.Error("函数CreateOrUpdateXmlAttributeByXPath,参数4,value->" + value);
}
return isSuccess;
}
///<summary>
/// 修改匹配XPath表达式的第一个节点中的对应得属性
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
///<returns>成功返回true,失败返回false</returns>
public static bool ModifyXmlAttributeByXPath(string xmlFileName, string xpath, string attributeName, string attributeValue)
{
if (string.IsNullOrEmpty(xmlFileName) || string.IsNullOrEmpty(xpath) || string.IsNullOrEmpty(attributeName))
{
return false;
}
bool isSuccess = false;
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
xmlNode.Attributes[attributeName].Value = attributeValue;
}
xmlDoc.Save(xmlFileName); //保存到XML文档
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数ModifyXmlAttributeByXPath,总参数2,错误信息->" + ex.Message);
log.Error("函数ModifyXmlAttributeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数ModifyXmlAttributeByXPath,参数2,xpath->" + xpath);
}
return isSuccess;
}
#endregion
#region XML文档节点或属性的删除
///<summary>
/// 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除)
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
///<returns>成功返回true,失败返回false</returns>
public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
{
if (string.IsNullOrEmpty(xmlFileName) || string.IsNullOrEmpty(xpath))
{
return false;
}
bool isSuccess = false;
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
//删除节点
xmlNode.ParentNode.RemoveChild(xmlNode);
}
xmlDoc.Save(xmlFileName); //保存到XML文档
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数DeleteXmlNodeByXPath,总参数2,错误信息->" + ex.Message);
log.Error("函数DeleteXmlNodeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数DeleteXmlNodeByXPath,参数2,xpath->" + xpath);
}
return isSuccess;
}
///<summary>
/// 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
///<param name="xmlAttributeName">要删除的xmlAttributeName的属性名称</param>
///<returns>成功返回true,失败返回false</returns>
public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
{
if (string.IsNullOrEmpty(xmlFileName) || string.IsNullOrEmpty(xpath) || string.IsNullOrEmpty(xpath))
{
return false;
}
bool isSuccess = false;
bool isExistsAttribute = false;
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
XmlAttribute xmlAttribute = null;
if (xmlNode != null)
{
//遍历xpath节点中的所有属性
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
{
//节点中存在此属性
xmlAttribute = attribute;
isExistsAttribute = true;
break;
}
}
if (isExistsAttribute)
{
//删除节点中的属性
xmlNode.Attributes.Remove(xmlAttribute);
}
}
xmlDoc.Save(xmlFileName); //保存到XML文档
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数DeleteXmlAttributeByXPath,总参数3,错误信息->" + ex.Message);
log.Error("函数DeleteXmlAttributeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数DeleteXmlAttributeByXPath,参数2,xpath->" + xpath);
log.Error("函数DeleteXmlAttributeByXPath,参数3,xmlAttributeName->" + xmlAttributeName);
}
return isSuccess;
}
///<summary>
/// 删除匹配XPath表达式的第一个节点中的所有属性
///</summary>
///<param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
///<param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
///<returns>成功返回true,失败返回false</returns>
public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
{
if (string.IsNullOrEmpty(xmlFileName) || string.IsNullOrEmpty(xpath))
{
return false;
}
bool isSuccess = false;
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
//遍历xpath节点中的所有属性
xmlNode.Attributes.RemoveAll();
}
xmlDoc.Save(xmlFileName); //保存到XML文档
isSuccess = true;
}
catch (Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(typeof(XmlTool));
log.Error("函数DeleteAllXmlAttributeByXPath,总参数2,错误信息->" + ex.Message);
log.Error("函数DeleteAllXmlAttributeByXPath,参数1,xmlFileName->" + xmlFileName);
log.Error("函数DeleteAllXmlAttributeByXPath,参数2,xpath->" + xpath);
}
return isSuccess;
}
#endregion
}