SoFunction
Updated on 2025-03-07

C# operation xml help class sharing (xml addition, deletion, modification and search)


using System;
using ;
using ;

namespace
{
public class XmlHelper
{
#region Public variables
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
#endregion

#region Create Xml Document
/// <summary>
/// Create an Xml file with root node
/// </summary>
/// <param name="FileName">Xml file name</param>
/// <param name="rootName">root node name</param>
/// <param name="Encode">Coding method: gb2312, UTF-8 and other common</param>
/// <param name="DirPath">Save directory path</param>
/// <returns></returns>
public bool CreateXmlDocument(string FileName, string RootName, string Encode)
{
try
{
xmldoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = ("1.0", Encode,null);
(xmldecl);
xmlelem = ("", RootName, "");
(xmlelem);
(FileName);
return true;
}
catch (Exception e)
{
return false;
throw new Exception();
}
}

#endregion

#region Common operation methods (addition, delete and modify)
/// <summary>
/// Insert a node and its several child nodes
/// </summary>
/// <param name="XmlFile">Xml file path</param>
/// <param name="NewNodeName">Inserted node name</param>
/// <param name="HasAttributes">Does this node have attributes, True is true, False is none</param>
/// <param name="fatherNode">The XPath expression to match for this insert node's parent node (for example: "//node name//child node name)</param>
/// <param name="htAtt">The attribute of this node, the Key is the attribute name, and the Value is the attribute value</param>
/// <param name="htSubNode">Properties of child nodes, Key is Name, Value is InnerText</param>
/// <returns>Return true is the update is successful, otherwise it will fail</returns>
public bool InsertNode(string XmlFile, string NewNodeName, bool HasAttributes, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
{
try
{
xmldoc = new XmlDocument();
(XmlFile);
XmlNode root = (fatherNode);
xmlelem = (NewNodeName);
if (htAtt != null && HasAttributes)//If this node has attributes, add the attribute first
{
SetAttributes(xmlelem, htAtt);
SetNodes(, xmldoc, xmlelem, htSubNode);//After adding this node property, add its child nodes and their InnerText
}
else
{
SetNodes(, xmldoc, xmlelem, htSubNode);//If this node has no attributes, then directly add its child nodes
}

(xmlelem);
(XmlFile);

return true;
}
catch (Exception e)
{

throw new Exception();

}
}
/// <summary>
/// Update node
/// </summary>
/// <param name="XmlFile">Xml file path</param>
/// <param name="fatherNode">The XPath expression to match with the node's upper node (for example: "//node name//child node name)</param>
/// <param name="htAtt">The attribute table that needs to be updated, Key represents the attribute that needs to be updated, Value represents the updated value</param>
/// <param name="htSubNode">The attribute table of the child nodes that need to be updated, the Key represents the child node name Name that needs to be updated, and the Value represents the updated value InnerText</param>
/// <returns>Return true is the update is successful, otherwise it will fail</returns>
public bool UpdateNode(string XmlFile, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
{
try
{
xmldoc = new XmlDocument();
(XmlFile);
XmlNodeList root = (fatherNode).ChildNodes;
UpdateNodes(root, htAtt, htSubNode);
(XmlFile);
return true;
}
catch (Exception e)
{
throw new Exception();
}
}

/// <summary>
/// Delete the child nodes under the specified node
/// </summary>
/// <param name="XmlFile">Xml file path</param>
/// <param name="fatherNode">Define the XPath expression to match the node (for example: "//node name//child node name)</param>
/// <returns>Return true is the update is successful, otherwise it will fail</returns>
public bool DeleteNodes(string XmlFile, string fatherNode)
{
try
{
xmldoc = new XmlDocument();
(XmlFile);
xmlnode = (fatherNode);
();
(XmlFile);
return true;
}
catch (XmlException xe)
{
throw new XmlException();
}
}

/// <summary>
/// Delete the first node that matches the XPath expression (child elements in the node will be deleted at the same time)
/// </summary>
/// <param name="xmlFileName">XML document complete file name (including physical path)</param>
/// <param name="xpath">XPath expression to match (for example: "//node name//child node name</param>
/// <returns>Return true for success, return false for failure</returns>
public bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
{
bool isSuccess = false;
xmldoc = new XmlDocument();
try
{
(xmlFileName); //Load XML document
XmlNode xmlNode = (xpath);
if (xmlNode != null)
{
//Delete node
(xmlNode);
}
(xmlFileName); //Save to XML document
isSuccess = true;
}
catch (Exception ex)
{
throw ex; //You can define your own exception handling here
}
return isSuccess;
}

/// <summary>
/// Delete the attribute of the matching parameter xmlAttributeName in the first node matching the XPath expression
/// </summary>
/// <param name="xmlFileName">XML document complete file name (including physical path)</param>
/// <param name="xpath">XPath expression to match (for example: "//node name//child node name</param>
/// <param name="xmlAttributeName">Attribute name of the xmlAttributeName to be deleted</param>
/// <returns>Return true for success, return false for failure</returns>
public bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
{
bool isSuccess = false;
bool isExistsAttribute = false;
xmldoc = new XmlDocument();
try
{
(xmlFileName); //Load XML document
XmlNode xmlNode = (xpath);
XmlAttribute xmlAttribute = null;
if (xmlNode != null)
{
//Transfer all properties in the xpath node
foreach (XmlAttribute attribute in )
{
if (() == ())
{
//This property exists in the node
xmlAttribute = attribute;
isExistsAttribute = true;
break;
}
}
if (isExistsAttribute)
{
//Delete the attributes in the node
(xmlAttribute);
}
}
(xmlFileName); //Save to XML document
isSuccess = true;
}
catch (Exception ex)
{
throw ex; //You can define your own exception handling here
}
return isSuccess;
}

/*Ke Leyi*/
/// <summary>
/// Delete all attributes in the first node matching the XPath expression
/// </summary>
/// <param name="xmlFileName">XML document complete file name (including physical path)</param>
/// <param name="xpath">XPath expression to match (for example: "//node name//child node name</param>
/// <returns>Return true for success, return false for failure</returns>
public bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
{
bool isSuccess = false;
xmldoc = new XmlDocument();
try
{
(xmlFileName); //Load XML document
XmlNode xmlNode = (xpath);
if (xmlNode != null)
{
//Transfer all properties in the xpath node
();
}
(xmlFileName); //Save to XML document
isSuccess = true;
}
catch (Exception ex)
{
throw ex; //You can define your own exception handling here
}
return isSuccess;
}
#endregion

#region Private Method
/// <summary>
/// Set node properties
/// </summary>
/// <param name="xe">Element where the node is located</param>
/// <param name="htAttribute">Node attribute, Key represents the attribute name, Value represents the attribute value</param>
private void SetAttributes(XmlElement xe, Hashtable htAttribute)
{
foreach (DictionaryEntry de in htAttribute)
{
((), ());
}
}
/// <summary>
/// Add child nodes to root node
/// </summary>
/// <param name="rootNode">Superior node name</param>
/// <param name="XmlDoc">Xml Documentation</param>
/// <param name="rootXe">Element to which the parent root node belongs</param>
/// <param name="SubNodes">SubNode attribute, Key is Name value, Value is InnerText value</param>
private void SetNodes(string rootNode, XmlDocument XmlDoc, XmlElement rootXe, Hashtable SubNodes)
{
if (SubNodes == null)
return;
foreach (DictionaryEntry de in SubNodes)
{
xmlnode = (rootNode);
XmlElement subNode = (());
= ();
(subNode);
}
}
/// <summary>
/// Update node properties and child node InnerText values. Ke Leyi
/// </summary>
/// <param name="root">root node name</param>
/// <param name="htAtt">Attribute name and value that needs to be changed</param>
/// <param name="htSubNode"> Need to change the child node name and value of InnerText</param>
private void UpdateNodes(XmlNodeList root, Hashtable htAtt, Hashtable htSubNode)
{
foreach (XmlNode xn in root)
{
xmlelem = (XmlElement)xn;
if ()//If a node is a property, change its properties first
{
foreach (DictionaryEntry de in htAtt)//Transfer the attribute hash table
{
if ((()))//If the node has properties that need to be changed
{
((), ());// Assign the corresponding value value in the hash table to this attribute Key
}
}
}
if ()//If there are child nodes, modify the InnerText of its child nodes
{
XmlNodeList xnl = ;
foreach (XmlNode xn1 in xnl)
{
XmlElement xe = (XmlElement)xn1;
foreach (DictionaryEntry de in htSubNode)
{
if ( == ())//The key in htSubNode stores the node name that needs to be changed.
{
= ();//The Value in htSubNode stores the updated data of the Key node
}
}
}
}

}
}
#endregion
#region XML document node query and read
/// <summary>
/// Select the first node XmlNode that matches the XPath expression.
/// </summary>
/// <param name="xmlFileName">XML document complete file name (including physical path)</param>
/// <param name="xpath">XPath expression to match (for example: "//node name//child node name")</param>
/// <returns>Return XmlNode</returns>
public XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)
{
xmldoc = new XmlDocument();
try
{
(xmlFileName); //Load XML document
XmlNode xmlNode = (xpath);
return xmlNode;
}
catch (Exception ex)
{
return null;
//throw ex; //You can define your own exception handling here
}
}

/// <summary>
/// Select the node list that matches the XPath expression XmlNodeList.
/// </summary>
/// <param name="xmlFileName">XML document complete file name (including physical path)</param>
/// <param name="xpath">XPath expression to match (for example: "//node name//child node name")</param>
/// <returns>Return XmlNodeList</returns>
public XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
{
xmldoc = new XmlDocument();
try
{
(xmlFileName); //Load XML document
XmlNodeList xmlNodeList = (xpath);
return xmlNodeList;
}
catch (Exception ex)
{
return null;
//throw ex; //You can define your own exception handling here
}
}

/// <summary>
/// Select the attribute of the matching xmlAttributeName of the first node matching the XPath expression. Ke Leyi
/// </summary>
/// <param name="xmlFileName">XML document complete file name (including physical path)</param>
/// <param name="xpath">XPath expression to match (for example: "//node name//child node name</param>
/// <param name="xmlAttributeName">To match the attribute name of the xmlAttributeName</param>
/// <returns>Return xmlAttributeName</returns>
public XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)
{
string content = ;
xmldoc = new XmlDocument();
XmlAttribute xmlAttribute = null;
try
{
(xmlFileName); //Load XML document
XmlNode xmlNode = (xpath);
if (xmlNode != null)
{
if ( > 0)
{
xmlAttribute = [xmlAttributeName];
}
}
}
catch (Exception ex)
{
throw ex; //You can define your own exception handling here
}
return xmlAttribute;
}
#endregion
}
}