C # recursively read and write XML files

Source: Internet
Author: User

Because the node formats of the XML files to be operated are not uniform, a general recursive class is written,CodeAs follows:

I. xml files

 
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <etetsions> <cycles> <sy_dt5_gsm_month> 5 </sy_dt5_gsm_month> <strong> 50 </strong> </cycles> </appsetsions> <transmissions> <GSM> <sy_dt5_gsm Company = "" model = "" com = "com3"> <yesterday> 2011-07-16 </yesterday> <lastmonth> </sy_dt5_gsm> </GSM> <GRPS> </GRPS> <ip> </IP> </transmissions> <connectionstrings> <Server>. </Server> <database> voltage </database> <user> SA </user> <pass> </connectionstrings> </configuration>

Ii. Recursion

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. XML; // <summary> /// description of the xmlrwrite abstract /// </Summary> public class xmlrwrite {private string _ filepath; private string _ parentnode; private string _ childname; private string _ attributename; Public xmlrwrite () {}/// <summary> /// XML file storage path // </Summary> Public String filepath {set {_ filepath = value ;}}// /<Summary> // parent node // </Summary> Public String parentnode {set {_ parentnode = value ;}} /// <summary> /// sub-node /// </Summary> Public String childname {set {_ childname = value ;}} /// <summary >/// node attributes /// </Summary> Public String attributename {set {_ attributename = value ;}} /// <summary> /// recursively read the XML file node value /// </Summary> /// <Param name = "xnl"> xmlnodelist list </param>/ // <returns> return node value </Re Turns> private string readnodevalue (xmlnodelist xnl) {string value = ""; for (INT I = 0; I <xnl. Count; I ++) {If (value! = "") Break; If (xnl [I]. name = _ parentnode) {foreach (xmlnode childnode in xnl [I]) {If (childnode. name = _ childname) {value = childnode. innertext; break ;}}else {xmlnodelist xnl = xnl [I]. childnodes; If (xnl. count> 0) value = readnodevalue (xnl) ;}} return value ;} /// <summary> /// recursively read the XML file node attribute value /// </Summary> /// <Param name = "xnl"> xmlnodelist list </param>/ // <returns> return node value </returns> priv Ate string readattributevalue (xmlnodelist xnl) {string value = ""; for (INT I = 0; I <xnl. Count; I ++) {If (value! = "") Break; If (xnl [I]. name = _ parentnode) {foreach (xmlnode childnode in xnl [I]) {If (childnode. name = _ childname) {value = childnode. attributes [_ attributename]. value; break ;}}else {xmlnodelist xnl = xnl [I]. childnodes; If (xnl. count> 0) value = readattributevalue (xnl) ;}} return value ;} /// <summary> /// recursively set the node value /// </Summary> /// <Param name = "value"> node value </param> /// <Param name = "xnl"> xmlnodelist list </param> private void writenodevalue (string value, xmlnodelist xnl) {bool BL = false; For (INT I = 0; I <xnl. count; I ++) {If (BL) break; If (xnl [I]. name = _ parentnode) {foreach (xmlnode childnode in xnl [I]) {If (childnode. name = _ childname) {childnode. innertext = value; BL = true; break ;}} else {xmlnodelist xnl = xnl [I]. childnodes; If (xnl. count> 0) writenodevalue (value, xnl );}}} /// <summary> /// recursively set the node attribute value /// </Summary> /// <Param name = "value"> attribute value </param> // <param name = "xnl"> xmlnodelist list </param> private void writeattributevalue (string value, xmlnodelist xnl) {bool BL = false; For (INT I = 0; I <xnl. count; I ++) {If (BL) break; If (xnl [I]. name = _ parentnode) {foreach (xmlnode childnode in xnl [I]) {If (childnode. name = _ childname) {childnode. attributes [_ attributename]. value = value; BL = true; break ;}} else {xmlnodelist xnl = xnl [I]. childnodes; If (xnl. count> 0) writeattributevalue (value, xnl) ;}} private xmlnodelist xnodelist () {xmldocument xmldoc = new xmldocument (); xmldoc. load (_ filepath); xmlnodelist xnl = xmldoc. selectsinglenode ("configuration "). childnodes; return xnl ;} /// <summary> /// recursively read the node value /// </Summary> /// <returns> return value </returns> Public String getnodevalue () {xmlnodelist xnl = xnodelist (); string value = readnodevalue (xnl); return value ;} /// <summary> /// recursively read the attribute value /// </Summary> /// <returns> return value </returns> Public String getattributevalue () {xmlnodelist xnl = xnodelist (); string value = readattributevalue (xnl); return value ;} /// <summary> /// recursively set the node value /// </Summary> /// <Param name = "value"> node value </param> Public void setnodevalue (string value) {xmldocument xmldoc = new xmldocument (); xmldoc. load (_ filepath); xmlnodelist xnl = xmldoc. selectsinglenode ("configuration "). childnodes; writenodevalue (value, xnl); xmldoc. save (_ filepath );} /// <summary> /// recursively set the node property value /// </Summary> /// <Param name = "value"> attribute value </param> Public void setattributevalue (string Value) {xmldocument xmldoc = new xmldocument (); xmldoc. load (_ filepath); xmlnodelist xnl = xmldoc. selectsinglenode ("configuration "). childnodes; writeattributevalue (value, xnl); xmldoc. save (_ filepath );}}

Iii. Usage

 
Protected void button#click (Object sender, eventargs e) {string Path = "C: \ Users \ Administrator \ Documents \ Visual Studio 2010 \ websites \ website2 \ setup. XML "; xmlrwrite = new xmlrwrite (); xmlrwrite. filepath = path; // read the node value xmlrwrite. parentnode = "sy_dt5_gsm"; xmlrwrite. childname = "yesterday"; lblmsg. TEXT = xmlrwrite. getnodevalue (); // read the attribute value xmlrwrite. parentnode = "GSM"; xmlrwrite. childname = "sy_dt5_gsm"; xmlrwrite. attributename = "com"; lblmsg. TEXT = xmlrwrite. getattributevalue (); // set the node value xmlrwrite. parentnode = "sy_dt5_gsm"; xmlrwrite. childname = "yesterday"; xmlrwrite. setnodevalue (""); // you can specify the xmlrwrite attribute. parentnode = "GSM"; xmlrwrite. childname = "sy_dt5_gsm"; xmlrwrite. attributename = "com"; xmlrwrite. setattributevalue ("com3 ");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.