Original article link
XML document operation instance
Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. XML; using system. collections; using system. io; public class xmlhealper {constructor # region constructor public xmlhealper () {filepath = xmlpath. getxmlpath (); openxml () ;}# endh Gion object definition # region object definition private xmldocument xmldoc = new xmldocument (); xmlnode; xmlelement xmlelem; # endregion attribute definition # region attribute definition private string errormess; public String errormess {get {return errormess;} set {errormess = value;} private string filepath; Public String filepath {set {filepath = value;} get {return filepath ;}} # endregion load XML file and create operation object # region load XML file and create operation object /**//// <Summary> // load the XML file and create an operation object // </Summary> Public void openxml () {try {If (! String. isnullorempty (filepath) {xmldoc. load (filepath);} else {filepath = string. format (@ "d :\\ xmlexample. XML "); // default path xmldoc. load (filepath) ;}} catch (exception ex) {errormess = ex ;}} # endregion create XML Document # region create XML document/*** // <summary> // create an XML file with a root node /// </Summary> // /<Param name = "FILENAME"> XML file name </param> // <Param name = "rootname"> root node name </param> // <Param name = "encode"> encoding method: gb23 12, UTF-8 and other common </param> /// <Param name = "dirpath"> saved directory path </param> /// <returns> </returns> Public bool createxmldocument (string filename, string rootname, string encode) {try {xmldeclaration xmldecl; xmldecl = xmldoc. createxmldeclaration ("1.0", encode, null); xmldoc. appendchild (xmldecl); xmlelem = xmldoc. createelement ("", rootname, ""); xmldoc. appendchild (xmlelem); xmldoc. save (filename); Return tru E;} catch (exception e) {return false ;}} # endregion get the data table # region get the data table/** // <summary> // get the data table // </Summary> // <returns> </returns> public dataview getdata () {xmldoc = new xmldocument (); dataset DS = new dataset (); stringreader READ = new stringreader (xmldoc. selectsinglenode (filepath ). outerxml); DS. readxml (read); Return Ds. tables [0]. defaultview;} # endregion reads the specified attribute value of a specified node # Set the property value/** // <summary> // function: /// read the specified attribute value of the specified node /// </Summary> /// <Param name = "strnode"> node name (relative path: // + node name) </param> /// <Param name = "strattribute"> attributes of this node </param> /// <returns> </returns> Public String getxmlnodevalue (string strnode, string strattribute) {string strreturn = ""; try {// obtain the node xmlnode = xmldoc Based on the specified path. selectsinglenode (strnode); // get the node attributes and cyclically retrieve the expected attribute values xmlattributecollection xmlatt R = xmlnode. attributes; For (INT I = 0; I <xmlattr. count; I ++) {If (xmlattr. item (I ). name = strattribute) {strreturn = xmlattr. item (I ). value ;}} catch (xmlexception xmle) {Throw xmle;} return strreturn ;} # endregion reads the value of a specified node # region reads the value of a specified node/*** // <summary> // reads the value of a specified node /// </Summary>/ // <Param name = "strnode"> node name </param> // <returns> </returns> Public String getxmlnodevalue (string strno De) {string strreturn = string. empty; try {// obtain the node xmlnode = xmldoc Based on the path. selectsinglenode (strnode); strreturn = xmlnode. innertext;} catch (xmlexception xmle) {system. console. writeline (xmle. message);} return strreturn ;} # endregion get the root element of the XML file # region get the root element of the XML file/** // <summary> // get the root element of the XML file /// </ summary> Public xmlnode getxmlroot () {return xmldoc. documentelement;} # obtain XM through endregion L node value # region get XML node value/*** // <summary> // get XML node value /// </Summary> /// <Param name = "nodename"> </param> // <returns> </returns> Public String getnodevalue (string nodename) {xmldoc = new xmldocument (); xmldoc. load (filepath); xmlnodelist xnl = xmldoc. childnodes; foreach (xmlnode xnf in xnl) {xmlelement Xe = (xmlelement) xnf; xmlnodelist xnf1 = Xe. childnodes; foreach (xmlnode xn2 in xnf1) {If (xn2. I Nnertext = nodename) {xmlelement xe2 = (xmlelement) xn2; return xe2.getattribute ("value") ;}} return NULL ;} # endregion set node value # region set node value/** // <summary> // function: /// set the node value /// </Summary> /// <Param name = "strnode"> node name </param> /// <Param name = "newvalue ""> node value </param> Public void setxmlnodevalue (string xmlnodepath, string xmlnodevalue) {try {// obtain the node xmlnode = xmldoc Based on the specified path. selectsingle Node (xmlnodepath); // set the node value xmlnode. innertext = xmlnodevalue;} catch (xmlexception xmle) {Throw xmle ;}} # Add a parent node to endregion # Add a parent node to Region/** // <summary> // Add a parent node to the root node // </Summary> Public void addparentnode (string parentnode) {xmlnode root = getxmlroot (); xmlnode parentxmlnode = xmldoc. createelement (parentnode); root. appendchild (parentxmlnode) ;}# endregion inserts a subnode into an existing parent node # region returns an existing Insert a subnode to the parent node/** /// <summary> /// insert a subnode to an existing parent node /// </Summary> Public void addchildnode (string parentnodepath, string childnodepath) {xmlnode parentxmlnode = xmldoc. selectsinglenode (parentnodepath); xmlnode childxmlnode = xmldoc. createelement (childnodepath); parentxmlnode. appendchild (childxmlnode );} # endregion add attributes to a node # region add attributes to a node/*** // <summary> // Add attributes to a node /// </Summary> Public Void addattribute (string nodepath, string nodeattribute) {xmlattribute nodeattribute = xmldoc. createattribute (nodeattribute); xmlnode nodepath = xmldoc. selectsinglenode (nodepath); nodepath. attributes. append (nodeattribute );} # insert an endregion node and Its subnodes # insert a node and Its subnodes/** // <summary> /// insert a node and Its subnodes /// </Summary> /// <Param name = "newnodename"> name of the inserted node </param> /// <Param name = "hasattribu Tes "> whether the node has attributes. True indicates yes, false: None </param> /// <Param name = "fathernode"> parent node of the inserted node </param> /// <Param name = "htatt"> node attributes, key is the attribute name, value is the attribute value </param> // <Param name = "htsubnode"> attribute of the subnode, key is name, if the value is innertext </param> /// <returns>, the update is successful. Otherwise, the update fails. </returns> Public bool insertnode (string newnodename, bool hasattributes, string fathernode, hashtable htatt, hashtable htsubnode) {try {xmldoc. load (filep ATH); xmlnode root = xmldoc. selectsinglenode (fathernode); xmlelem = xmldoc. createelement (newnodename); If (htatt! = NULL & hasattributes) // if this node has an attribute, add the attribute {setattributes (xmlelem, htatt); addnodes (xmlelem. name, xmldoc, xmlelem, htsubnode); // after adding this node attribute, add its subnodes and their innertext} else {addnodes (xmlelem. name, xmldoc, xmlelem, htsubnode); // if this node has no attribute, add its subnode directly} root. appendchild (xmlelem); xmldoc. save (filepath); Return true;} catch (exception e) {Throw new exception (E. message );}} # endregion set node attributes # region set node attributes/** // <summary> // set node attributes /// </Summary> // <Param name = "XE"> element of the node </param> // <Param name = "htattribute"> node attribute, key indicates the attribute name, and value indicates the attribute value </param> Public void setattributes (xmlelement XE, hashtable htattribute) {foreach (dictionaryentry de in htattribute) {Xe. setattribute (de. key. tostring (), de. value. tostring ());}} # Add a subnode to the root node for endregion # Add a subnode to the root node for Region/*** // <summary> // Add a subnode to the root node // // </Summary> /// <Param name = "rootnode"> parent node name </param> /// <Param name = "xmldoc"> XML document </param> /// <Param name = "rootxe"> element to which the parent root node belongs </param> /// <Param name = "subnodes"> subnode attributes, key is the name value, value is the innertext value </param> Public void addnodes (string rootnode, xmldocument xmldoc, xmlelement rootxe, hashtable subnodes) {foreach (dictionaryentry de in subnodes) {xmlnode = xmldoc. selectsinglenode (rootnode); xmlelement subnode = xmldoc. createelement (de. key. tostring (); subnode. innertext = de. value. tostring (); rootxe. appendchild (subnode) ;}# endregion // update the attribute value of the configured node # region sets the attribute value of the node/*** // <summary> /// function: /// set the node attribute value /// </Summary> /// <Param name = "xmlnodepath"> node name </param> /// <Param name = "xmlnodeattribute "> attribute name </param> // <Param name =" xmlnodeattributevalue "> attribute value </param> Public void setxmlnodevalue (string xmlnodepath, string xmlnodeattribute, string xmlnodeattributevalue) {try {// obtain the node xmlnode = xmldoc Based on the specified path. selectsinglenode (xmlnodepath); // obtain the node attributes and cyclically retrieve the expected attribute values xmlattributecollection xmlattr = xmlnode. attributes; For (INT I = 0; I <xmlattr. count; I ++) {If (xmlattr. item (I ). name = xmlnodeattribute) {xmlattr. item (I ). value = xmlnodeattributevalue; break ;}} catch (xmlexception xmle) {Throw xmle ;}} # endregion update node # region update node/** // <summary> // update node // </Summary> // <Param name = "fathernode"> The parent node of the node to be updated </param> // <Param name = "htatt"> the Attribute Table to be updated, key indicates the attribute to be updated, and value indicates the updated value </param> // <Param name = "htsubnode"> Attribute Table of the child node to be updated, key indicates the name of the child node to be updated, and value indicates the updated value innertext </param> // <returns> indicates that the update is successful, otherwise it fails </returns> Public bool updatenode (string fathernode, hashtable htatt, hashtable htsubnode) {try {xmldoc = new xmldocument (); xmldoc. load (filepath); xmlnodelist root = xmldoc. selectsinglenode (fathernode ). childnodes; updatenodes (root, htatt, htsubnode); xmldoc. save (filepath); Return true;} catch (exception e) {Throw new exception (E. message );}} # endregion update node attributes and subnode innertext values # region update node attributes and subnode innertext values/*** // <summary> // update node attributes and subnode innertext value /// </Summary> /// <Param name = "root"> root node name </param> /// <Param name = "htatt"> attribute name and value </param> /// <Param name = "htsubnode"> you need to change the name and value of the innertext subnode </param> Public void updatenodes (xmlnodelist root, hashtable htatt, hashtable htsubnode) {foreach (xmlnode Xn in root) {xmlelem = (xmlelement) xn; If (xmlelem. hasattributes) // If a node is an attribute, first change its attribute {foreach (dictionaryentry de in htatt) // traverse the attribute hash table {If (xmlelem. hasattribute (de. key. tostring () // If the node has the attribute to be changed {xmlelem. setattribute (de. key. tostring (), de. value. tostring (); // The value of the hash table is assigned to this attribute key }}} if (xmlelem. haschildnodes) // If a subnode exists, modify the innertext {xmlnodelist xnl = xmlelem of the subnode. childnodes; foreach (xmlnode xn1 in xnl) {xmlelement Xe = (xmlelement) xn1; foreach (dictionaryentry de in htsubnode) {If (Xe. name = de. key. tostring () // The key in htsubnode stores the node name to be changed, {Xe. innertext = de. value. tostring (); // The value in htsubnode stores the updated data of the key node }}}}# endregion deletes the attribute of a node # region deletes the attribute of a node /**//// <summary> /// Delete the attributes of a node /// </Summary> Public void deleteattribute (string nodepath, string nodeattribute, string nodeattributevalue) {xmlnodelist nodepath = xmldoc. selectsinglenode (nodepath ). childnodes; foreach (xmlnode Xn in nodepath) {xmlelement Xe = (xmlelement) xn; If (Xe. getattribute (nodeattribute) = nodeattributevalue) {Xe. removeattribute (nodeattribute ); // Delete attribute }}# endregion delete a node # region delete a node/*** // <summary> /// delete a node /// </Summary> public void deletexmlnode (string tempxmlnode) {xmlnode xmlnodepath = xmldoc. selectsinglenode (tempxmlnode); xmlnodepath. parentnode. removechild (xmlnodepath );} # endregion delete a subnode under a specified node # region delete a subnode under a specified node/** // <summary> /// delete a subnode under a specified node // // </Summary> /// <Param name = "fathernode"> specify a node </param> /// <returns> indicates that the update is successful, otherwise it fails </returns> Public bool deletenodes (string fathernode) {try {xmldoc = new xmldocument (); xmldoc. load (filepath); xmlnode = xmldoc. selectsinglenode (fathernode); xmlnode. removeall (); xmldoc. save (filepath); Return true;} catch (xmlexception Xe) {Throw new xmlexception (Xe. message) ;}# endregion private function # region private function private string functionreturn (xmlnodelist xmllist, int I, string nodename) {string node = xmllist [I]. tostring (); string rusultnode = ""; for (Int J = 0; j <I; j ++) {If (node = nodename) {rusultnode = node. tostring ();} else {If (xmllist [J]. haschildnodes) {functionreturn (xmllist, J, nodename) ;}else {break ;}} return rusultnode ;} # endregion save XML file # region save XML file/*** // <summary> // function: /// Save the XML file ///// </Summary> Public void savexmldocument () {try {xmldoc. save (filepath);} catch (xmlexception xmle) {Throw xmle;}/** // <summary> // function: save the XML file /// </Summary> /// <Param name = "tempxmlfilepath"> </param> Public void savexmldocument (string tempxmlfilepath) {try {xmldoc. save (tempxmlfilepath);} catch (xmlexception xmle) {Throw xmle ;}# endregion}
C # XML document operations