c operation xml help class share of xml add delete change and check

  • 2020-05-30 20:57:19
  • OfStack


using System;
using System.Collections;
using System.Xml;
namespace Jb51.Com.XmlDAL
{
public class XmlHelper
{
#region  Public variables 
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
#endregion
#region  create Xml The document 
/// <summary>
///  create 1 One with a root node Xml file 
/// </summary>
/// <param name="FileName">Xml The file name </param>
/// <param name="rootName"> Root node name </param>
/// <param name="Encode"> encoding :gb2312 . UTF-8 Such as common </param>
/// <param name="DirPath"> The directory path to save </param>
/// <returns></returns>
public bool CreateXmlDocument(string FileName, string RootName, string Encode)
{
try
{
xmldoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0", Encode,null);
xmldoc.AppendChild(xmldecl);
xmlelem = xmldoc.CreateElement("", RootName, "");
xmldoc.AppendChild(xmlelem);
xmldoc.Save(FileName);
return true;
}
catch (Exception e)
{
return false;
throw new Exception(e.Message);
}
}
#endregion
#region  Common operating method ( Increases the deletion )
/// <summary>
///  insert 1 And its child nodes 
/// </summary>
/// <param name="XmlFile">Xml The file path </param>
/// <param name="NewNodeName"> The name of the inserted node </param>
/// <param name="HasAttributes"> Whether this node has an attribute, True To have, False For there is no </param>
/// <param name="fatherNode"> The parent of this inserted node , To match the XPath expression ( For example, :"// The node name // The child node name )</param>
/// <param name="htAtt"> The properties of this node, Key Is the property name, Value For the attribute value </param>
/// <param name="htSubNode"> Properties of child nodes, Key for Name,Value for InnerText</param>
/// <returns> The update was successful or failed </returns>
public bool InsertNode(string XmlFile, string NewNodeName, bool HasAttributes, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
{
try
{
xmldoc = new XmlDocument();
xmldoc.Load(XmlFile);
XmlNode root = xmldoc.SelectSingleNode(fatherNode);
xmlelem = xmldoc.CreateElement(NewNodeName);
if (htAtt != null && HasAttributes)// If this node has properties, add them first 
{
SetAttributes(xmlelem, htAtt);
SetNodes(xmlelem.Name, xmldoc, xmlelem, htSubNode);// After adding this node property, add its children and their InnerText
}
else
{
SetNodes(xmlelem.Name, xmldoc, xmlelem, htSubNode);// If the node has no attributes, add its children directly 
}
root.AppendChild(xmlelem);
xmldoc.Save(XmlFile);
return true;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
///  Update the node 
/// </summary>
/// <param name="XmlFile">Xml The file path </param>
/// <param name="fatherNode"> The parent node of the node needs to be updated , To match the XPath expression ( For example, :"// The node name // The child node name )</param>
/// <param name="htAtt"> Property sheets that need to be updated, Key Represents the properties that need to be updated, Value Represents the updated value </param>
/// <param name="htSubNode"> The property sheet of the child node that needs to be updated, Key Represents the name of the child node that needs to be updated Name,Value Represents the updated value InnerText</param>
/// <returns> The update was successful or failed </returns>
public bool UpdateNode(string XmlFile, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
{
try
{
xmldoc = new XmlDocument();
xmldoc.Load(XmlFile);
XmlNodeList root = xmldoc.SelectSingleNode(fatherNode).ChildNodes;
UpdateNodes(root, htAtt, htSubNode);
xmldoc.Save(XmlFile);
return true;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
///  Deletes child nodes under the specified node 
/// </summary>
/// <param name="XmlFile">Xml The file path </param>
/// <param name="fatherNode"> Set the node , To match the XPath expression ( For example, :"// The node name // The child node name )</param>
/// <returns> The update was successful or failed </returns>
public bool DeleteNodes(string XmlFile, string fatherNode)
{
try
{
xmldoc = new XmlDocument();
xmldoc.Load(XmlFile);
xmlnode = xmldoc.SelectSingleNode(fatherNode);
xmlnode.RemoveAll();
xmldoc.Save(XmlFile);
return true;
}
catch (XmlException xe)
{
throw new XmlException(xe.Message);
}
}
/// <summary>
///  Delete the match XPath Regulation of expression 1 A node ( Child elements in the node are deleted at the same time )
/// </summary>
/// <param name="xmlFileName">XML Full file name of document ( Including physical paths )</param>
/// <param name="xpath"> To match the XPath expression ( For example, :"// The node name // The child node name </param>
/// <returns> Successfully returns true, Failure to return false</returns>
public bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
{
bool isSuccess = false;
xmldoc = new XmlDocument();
try
{
xmldoc.Load(xmlFileName); // loading XML The document 
XmlNode xmlNode = xmldoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
// Remove nodes 
xmldoc.ParentNode.RemoveChild(xmlNode);
}
xmldoc.Save(xmlFileName); // Save to XML The document 
isSuccess = true;
}
catch (Exception ex)
{
throw ex; // Here you can define your own exception handling 
}
return isSuccess;
}
/// <summary>
///  Delete the match XPath Regulation of expression 1 The matching parameter in each node xmlAttributeName The properties of the 
/// </summary>
/// <param name="xmlFileName">XML Full file name of document ( Including physical paths )</param>
/// <param name="xpath"> To match the XPath expression ( For example, :"// The node name // The child node name </param>
/// <param name="xmlAttributeName"> Want to delete the xmlAttributeName Property name of </param>
/// <returns> Successfully returns true, Failure to return false</returns>
public bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
{
bool isSuccess = false;
bool isExistsAttribute = false;
xmldoc = new XmlDocument();
try
{
xmldoc.Load(xmlFileName); // loading XML The document 
XmlNode xmlNode = xmldoc.SelectSingleNode(xpath);
XmlAttribute xmlAttribute = null;
if (xmlNode != null)
{
// traverse xpath All properties in the node 
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
{
// This property exists in the node 
xmlAttribute = attribute;
isExistsAttribute = true;
break;
}
}
if (isExistsAttribute)
{
// Delete the properties in the node 
xmlNode.Attributes.Remove(xmlAttribute);
}
}
xmldoc.Save(xmlFileName); // Save to XML The document 
isSuccess = true;
}
catch (Exception ex)
{
throw ex; // Here you can define your own exception handling 
}
return isSuccess;
}
/* KeLeYi */
/// <summary>
///  Delete the match XPath Regulation of expression 1 All properties in a node 
/// </summary>
/// <param name="xmlFileName">XML Full file name of document ( Including physical paths )</param>
/// <param name="xpath"> To match the XPath expression ( For example, :"// The node name // The child node name </param>
/// <returns> Successfully returns true, Failure to return false</returns>
public bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
{
bool isSuccess = false;
xmldoc = new XmlDocument();
try
{
xmldoc.Load(xmlFileName); // loading XML The document 
XmlNode xmlNode = xmldoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
// traverse xpath All properties in the node 
xmlNode.Attributes.RemoveAll();
}
xmldoc.Save(xmlFileName); // Save to XML The document 
isSuccess = true;
}
catch (Exception ex)
{
throw ex; // Here you can define your own exception handling 
}
return isSuccess;
}
#endregion
#region  Private methods 
/// <summary>
///  Set node properties 
/// </summary>
/// <param name="xe"> Where the node is located Element</param>
/// <param name="htAttribute"> Node properties, Key Stands for property name, Value Represents the attribute value </param>
private void SetAttributes(XmlElement xe, Hashtable htAttribute)
{
foreach (DictionaryEntry de in htAttribute)
{
xe.SetAttribute(de.Key.ToString(), de.Value.ToString());
}
}
/// <summary>
///  Add child nodes under the root node 
/// </summary>
/// <param name="rootNode"> Parent node name </param>
/// <param name="XmlDoc">Xml The document </param>
/// <param name="rootXe"> Belonging to the parent root node Element</param>
/// <param name="SubNodes"> Child node properties, Key for Name Value, Value for InnerText value </param>
private void SetNodes(string rootNode, XmlDocument XmlDoc, XmlElement rootXe, Hashtable SubNodes)
{
if (SubNodes == null)
return;
foreach (DictionaryEntry de in SubNodes)
{
xmlnode = XmlDoc.SelectSingleNode(rootNode);
XmlElement subNode = XmlDoc.CreateElement(de.Key.ToString());
subNode.InnerText = de.Value.ToString();
rootXe.AppendChild(subNode);
}
}
/// <summary>
///  Update the node properties and child nodes InnerText Value. ke   le   The righteous 
/// </summary>
/// <param name="root"> Root node name </param>
/// <param name="htAtt"> The property name and value that you need to change </param>
/// <param name="htSubNode"> You need to change InnerText Child node name and value </param>
private void UpdateNodes(XmlNodeList root, Hashtable htAtt, Hashtable htSubNode)
{
foreach (XmlNode xn in root)
{
xmlelem = (XmlElement)xn;
if (xmlelem.HasAttributes)// If the node is a property, change its properties first 
{
foreach (DictionaryEntry de in htAtt)// Traversing the property hash table 
{
if (xmlelem.HasAttribute(de.Key.ToString()))// If the node has properties that need to be changed 
{
xmlelem.SetAttribute(de.Key.ToString(), de.Value.ToString());// Then you take the corresponding value in the hash table Value Assign this property Key
}
}
}
if (xmlelem.HasChildNodes)// If there are child nodes, modify the child node's InnerText
{
XmlNodeList xnl = xmlelem.ChildNodes;
foreach (XmlNode xn1 in xnl)
{
XmlElement xe = (XmlElement)xn1;
foreach (DictionaryEntry de in htSubNode)
{
if (xe.Name == de.Key.ToString())//htSubNode In the key Store the node name that needs to be changed, 
{
xe.InnerText = de.Value.ToString();//htSubNode In the Value Store the Key Node update data 
}
}
}
}
}
}
#endregion
#region XML Document nodes query and read 
/// <summary>
///  Select match XPath Regulation of expression 1 A node XmlNode.
/// </summary>
/// <param name="xmlFileName">XML Full file name of document ( Including physical paths )</param>
/// <param name="xpath"> To match the XPath expression ( For example, :"// The node name // The child node name ")</param>
/// <returns> return XmlNode</returns>
public XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)
{
xmldoc = new XmlDocument();
try
{
xmldoc.Load(xmlFileName); // loading XML The document 
XmlNode xmlNode = xmldoc.SelectSingleNode(xpath);
return xmlNode;
}
catch (Exception ex)
{
return null;
//throw ex; // Here you can define your own exception handling 
}
}
/// <summary>
///  Select match XPath A list of nodes for an expression XmlNodeList.
/// </summary>
/// <param name="xmlFileName">XML Full file name of document ( Including physical paths )</param>
/// <param name="xpath"> To match the XPath expression ( For example, :"// The node name // The child node name ")</param>
/// <returns> return XmlNodeList</returns>
public XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
{
xmldoc = new XmlDocument();
try
{
xmldoc.Load(xmlFileName); // loading XML The document 
XmlNodeList xmlNodeList = xmldoc.SelectNodes(xpath);
return xmlNodeList;
}
catch (Exception ex)
{
return null;
//throw ex; // Here you can define your own exception handling 
}
}
/// <summary>
///  Select match XPath Regulation of expression 1 The matching of nodes xmlAttributeName The properties of the XmlAttribute.  KeLeYi 
/// </summary>
/// <param name="xmlFileName">XML Full file name of document ( Including physical paths )</param>
/// <param name="xpath"> To match the XPath expression ( For example, :"// The node name // The child node name </param>
/// <param name="xmlAttributeName"> To match xmlAttributeName Property name of </param>
/// <returns> return xmlAttributeName</returns>
public XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)
{
string content = string.Empty;
xmldoc = new XmlDocument();
XmlAttribute xmlAttribute = null;
try
{
xmldoc.Load(xmlFileName); // loading XML The document 
XmlNode xmlNode = xmldoc.SelectSingleNode(xpath);
if (xmlNode != null)
{
if (xmlNode.Attributes.Count > 0)
{
xmlAttribute = xmlNode.Attributes[xmlAttributeName];
}
}
}
catch (Exception ex)
{
throw ex; // Here you can define your own exception handling 
}
return xmlAttribute;
}
#endregion
}
}

How does this class work? Here is an example of creating an xml document:


XmlHelper m_menu_keleyi_com = new XmlHelper();
m_menu_keleyi_com.CreateXmlDocument(@"D:\kel"+"eyimenu.xml", "ke"+"leyimenu", "utf-8");

This short code creates a document named keleyimenu.xml on the D disk. The document has the root node keleyimenu, and the content of the document is:


<?xml version="1.0" encoding="utf-8"?>
<keleyimenu />


Related articles: