This article describes the C# custom processing of xml data classes. Share it for your reference. The specific analysis is as follows:
This C# class is dedicated to users processing xml data, which can greatly simplify the operation of xml. The class encapsulates commonly used xml operations, including opening and reading xml data, reading and writing node data, reading node data through xpath, exporting node data, etc., and can also expand the functions of the class by yourself as needed.
using System; using ; using ; using ; namespace { public class XMLProcess { #region constructor public XMLProcess() { } public XMLProcess(string strPath) { this._XMLPath = strPath; } #endregion #region Public properties private string _XMLPath; public string XMLPath { get { return this._XMLPath; } } #endregion #region Private Method /// <summary> /// Import XML file /// </summary> /// <param name="XMLPath">XML file path</param> private XmlDocument XMLLoad() { string XMLFile = XMLPath; XmlDocument xmldoc = new XmlDocument(); try { string filename = () + XMLFile; if ((filename)) (filename); } catch (Exception e) { } return xmldoc; } /// <summary> /// Import XML file /// </summary> /// <param name="XMLPath">XML file path</param> private static XmlDocument XMLLoad(string strPath) { XmlDocument xmldoc = new XmlDocument(); try { string filename = () + strPath; if ((filename)) (filename); } catch (Exception e) { } return xmldoc; } /// <summary> /// Return to the full path /// </summary> /// <param name="strPath">Xml path</param> private static string GetXmlFullPath(string strPath) { if ((":") > 0) { return strPath; } else { return (strPath); } } #endregion #region Read data /// <summary> /// Read data from the specified node /// </summary> /// <param name="node">node</param> /// Use the column: /// ("/Node", "") /// ("/Node/Element[@Attribute='Name']") public string Read(string node) { string value = ""; try { XmlDocument doc = XMLLoad(); XmlNode xn = (node); value = ; } catch { } return value; } /// <summary> /// Read the concatenated value of the specified path and node /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="attribute">Attribute name, return the attribute value when it is not empty, otherwise return the concatenated value</param> /// Use the column: /// (path, "/Node", "") /// (path, "/Node/Element[@Attribute='Name']") public static string Read(string path, string node) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = (node); value = ; } catch { } return value; } /// <summary> /// Read the attribute values of the specified path and node /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="attribute">Attribute name, return the attribute value when it is not empty, otherwise return the concatenated value</param> /// Use the column: /// (path, "/Node", "") /// (path, "/Node/Element[@Attribute='Name']", "Attribute") public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = (node); value = (("") ? : [attribute].Value); } catch { } return value; } /// <summary> /// Get the values of all child nodes of a certain node /// </summary> /// <param name="node">Node to query</param> public string[] ReadAllChildallValue(string node) { int i = 0; string[] str = { }; XmlDocument doc = XMLLoad(); XmlNode xn = (node); XmlNodeList nodelist = ; //Get the child node of this node if ( > 0) { str = new string[]; foreach (XmlElement el in nodelist)//Read element value { str[i] = ; i++; } } return str; } /// <summary> /// Get the values of all child nodes of a certain node /// </summary> /// <param name="node">Node to query</param> public XmlNodeList ReadAllChild(string node) { XmlDocument doc = XMLLoad(); XmlNode xn = (node); XmlNodeList nodelist = ; //Get the child node of this node return nodelist; } /// <summary> /// Read XML to return the sorted or filtered DataView /// </summary> /// <param name="strWhere">Filter conditions, such as: "name='kgdiwss'"</param> /// <param name="strSort"> sorting conditions, such as: "Id desc"</param> public DataView GetDataViewByXml(string strWhere, string strSort) { try { string XMLFile = ; string filename = () + XMLFile; DataSet ds = new DataSet(); (filename); DataView dv = new DataView([0]); //Create DataView to complete sorting or filtering operations if (strSort != null) { = strSort; //Sort the records in the DataView } if (strWhere != null) { = strWhere; //Filter the records in the DataView and find the records we want } return dv; } catch (Exception) { return null; } } /// <summary> ///Read XML to return DataSet /// </summary> /// <param name="strXmlPath">XML file relative path</param> public DataSet GetDataSetByXml(string strXmlPath) { try { DataSet ds = new DataSet(); (GetXmlFullPath(strXmlPath)); if ( > 0) { return ds; } return null; } catch (Exception) { return null; } } #endregion #region Insert data /// <summary> /// Insert data /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="element">Element name, insert a new element when it is not empty, otherwise insert an attribute in this element</param> /// <param name="attribute">Attribute name, insert the attribute value of the element when it is not empty, otherwise insert the element value</param> /// <param name="value">value</param> /// Use the column: /// (path, "/Node", "Element", "", "Value") /// (path, "/Node", "Element", "Attribute", "Value") /// (path, "/Node", "", "Attribute", "Value") public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); (() + path); XmlNode xn = (node); if (("")) { if (!("")) { XmlElement xe = (XmlElement)xn; (attribute, value); } } else { XmlElement xe = (element); if (("")) = value; else (attribute, value); (xe); } (() + path); } catch { } } /// <summary> /// Insert data /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="element">Element name, insert a new element when it is not empty, otherwise insert an attribute in this element</param> /// <param name="strList">Two-dimensional array composed of XML attribute names and values</param> public static void Insert(string path, string node, string element, string[][] strList) { try { XmlDocument doc = new XmlDocument(); (() + path); XmlNode xn = (node); XmlElement xe = (element); string strAttribute = ""; string strValue = ""; for (int i = 0; i < ; i++) { for (int j = 0; j < strList[i].Length; j++) { if (j == 0) strAttribute = strList[i][j]; else strValue = strList[i][j]; } if (("")) = strValue; else (strAttribute, strValue); } (xe); (() + path); } catch { } } /// <summary> /// Insert a row of data /// </summary> /// <param name="strXmlPath">XML file relative path</param> /// <param name="Columns">Array of column names to insert rows, such as: string[] Columns = {"name","IsMarried"};</param> /// <param name="ColumnValue">Array of value for each column of the row, such as: string[] ColumnValue={"XML Collection","false"};</param> /// <returns>Return true successfully, otherwise return false</returns> public static bool WriteXmlByDataSet(string strXmlPath, string[] Columns, string[] ColumnValue) { try { //Get the path of .XSD based on the incoming XML path, and the two files are placed in the same directory string strXsdPath = (0, (".")) + ".xsd"; DataSet ds = new DataSet(); (GetXmlFullPath(strXsdPath)); //Read XML schema, related to the column data type (GetXmlFullPath(strXmlPath)); DataTable dt = [0]; DataRow newRow = (); //Create a new row based on the original table for (int i = 0; i < ; i++) //Loop to assign values to each column in a row { newRow[Columns[i]] = ColumnValue[i]; } (newRow); (); (); (GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } #endregion #region Modify data /// <summary> /// Modify the data of the specified node /// </summary> /// <param name="node">node</param> /// <param name="value">value</param> public void Update(string node, string value) { try { XmlDocument doc = XMLLoad(); XmlNode xn = (node); = value; (() + XMLPath); } catch { } } /// <summary> /// Modify the data of the specified node /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="value">value</param> /// Use the column: /// (path, "/Node","Value") /// (path, "/Node","Value") public static void Update(string path, string node, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = (node); = value; (() + path); } catch { } } /// <summary> /// Modify the attribute value of the specified node (static) /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="attribute">Attribute name, modify the attribute value of the node when it is not empty, otherwise modify the node value</param> /// <param name="value">value</param> /// Use the column: /// (path, "/Node", "", "Value") /// (path, "/Node", "Attribute", "Value") public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = (node); XmlElement xe = (XmlElement)xn; if (("")) = value; else (attribute, value); (() + path); } catch { } } /// <summary> /// Change a record that meets the criteria /// </summary> /// <param name="strXmlPath">XML file path</param> /// <param name="Columns">Column name array</param> /// <param name="ColumnValue">Array of column values</param> /// <param name="strWhereColumnName">condition column name</param> /// <param name="strWhereColumnValue">condition column value</param> public static bool UpdateXmlRow(string strXmlPath, string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue) { try { string strXsdPath = (0, (".")) + ".xsd"; DataSet ds = new DataSet(); (GetXmlFullPath(strXsdPath));//Read XML schema, related to the column data type (GetXmlFullPath(strXmlPath)); //Judge the number of rows first if ([0]. > 0) { for (int i = 0; i < [0].; i++) { //If the current record is a record that meets the Where conditions if ([0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue)) { //Loop assign new values to each column that finds row for (int j = 0; j < ; j++) { [0].Rows[i][Columns[j]] = ColumnValue[j]; } (); //Update DataSet (GetXmlFullPath(strXmlPath));//Rewrite to XML file return true; } } } return false; } catch (Exception) { return false; } } #endregion #region Delete data /// <summary> /// Delete node value /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="attribute">Attribute name, delete the node attribute value when it is not empty, otherwise delete the node value</param> /// <param name="value">value</param> /// Use the column: /// (path, "/Node", "") /// (path, "/Node", "Attribute") public static void Delete(string path, string node) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = (node); (xn); (() + path); } catch { } } /// <summary> /// Delete data /// </summary> /// <param name="path">path</param> /// <param name="node">node</param> /// <param name="attribute">Attribute name, delete the node attribute value when it is not empty, otherwise delete the node value</param> /// <param name="value">value</param> /// Use the column: /// (path, "/Node", "") /// (path, "/Node", "Attribute") public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = (node); XmlElement xe = (XmlElement)xn; if (("")) (xn); else (attribute); (() + path); } catch { } } /// <summary> /// Delete all rows /// </summary> /// <param name="strXmlPath">XML Path</param> public static bool DeleteXmlAllRows(string strXmlPath) { try { DataSet ds = new DataSet(); (GetXmlFullPath(strXmlPath)); if ([0]. > 0) { [0].(); } (GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } /// <summary> /// Rewrite XML to delete the specified row by deleting the specified row in the DataSet and implementing the deleted row. /// </summary> /// <param name="iDeleteRow">Index value of the row to be deleted in the DataSet</param> public static bool DeleteXmlRowByIndex(string strXmlPath, int iDeleteRow) { try { DataSet ds = new DataSet(); (GetXmlFullPath(strXmlPath)); if ([0]. > 0) { [0].Rows[iDeleteRow].Delete(); } (GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } /// <summary> /// Delete rows with specified values in the specified column /// </summary> /// <param name="strXmlPath">XML relative path</param> /// <param name="strColumn">Column name</param> /// <param name="ColumnValue">Specify value</param> public static bool DeleteXmlRows(string strXmlPath, string strColumn, string[] ColumnValue) { try { DataSet ds = new DataSet(); (GetXmlFullPath(strXmlPath)); if ([0]. > 0) { //Judge whether there are many rows or deleted values, put the more for loops in it if ( > [0].) { for (int i = 0; i < [0].; i++) { for (int j = 0; j < ; j++) { if ([0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j])) { [0].Rows[i].Delete(); } } } } else { for (int j = 0; j < ; j++) { for (int i = 0; i < [0].; i++) { if ([0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j])) { [0].Rows[i].Delete(); } } } } (GetXmlFullPath(strXmlPath)); } return true; } catch (Exception) { return false; } } #endregion } }
I hope this article will be helpful to everyone's C# programming.