Use C # to add, delete, modify, and query XML

Source: Internet
Author: User

XML file format

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <messagelist> <br/> <message id = "1"> <br/> <Name> pandatv </Name> <br/> <QQ> 123456 </QQ> <br/> <email> panda@qq.com </Email> <br/> <content> asfdsafasdfasdfsad I am content </content> <br/> <time> </ time> <br/> </message> <br/> </messagelist>

 

Add >>>>

Xmldocument xmld = new xmldocument (); <br/> xmld. load (server. mappath ("app_code/message. XML "); // load the XML file <br/> xmlnode root = xmld. selectsinglenode ("messagelist"); // find the messagelist node </P> <p> int I = xmld. selectsinglenode ("messagelist "). childnodes. count + 1; <br/> xmlelement xml0 = xmld. createelement ("message"); <br/> xml0.setattribute ("ID", I. tostring (); </P> <p> xmlelement xml1 = xmld. createelement ("name"); // create a Name node <br/> xml1.innertext = this.txt name. value. trim (); <br/> xml0.appendchild (xml1); // Add </P> <p> xmlelement xml2 = xmld. createelement ("QQ"); // create a QQ node <br/> xml2.innertext = this.txt QQ. value. trim (); <br/> xml0.appendchild (xml2); // Add </P> <p> xmlelement xml3 = xmld. createelement ("email"); // create an email node <br/> xml3.innertext = this.txt email. value. trim (); <br/> xml0.appendchild (xml3); // Add </P> <p> xmlelement xml4 = xmld. createelement ("content"); // create a content node <br/> xml4.innertext = this.txt content. value; <br/> xml0.appendchild (xml4); // Add </P> <p> xmlelement xml5 = xmld. createelement ("time"); // create a time node <br/> xml5.innertext = datetime. now. tostring ("yyyy-mm-dd"); <br/> xml0.appendchild (xml5); // Add </P> <p> root. appendchild (xml0); </P> <p> xmld. save (server. mappath ("app_code/message. XML "));

 

Delete >>>>

Xmldocument xmld = new xmldocument (); </P> <p> xmld. load (server. mappath ("app_code/message. XML "); </P> <p> xmlnodelist list = xmld. selectsinglenode ("messagelist "). childnodes; </P> <p> foreach (xmlnode VaR in list) <br/>{< br/> xmlelement xmle = (xmlelement) var; </P> <p> If (xmle. getattribute ("ID") = ID) // If the ID is equal to the value of the text, delete <br/>{< br/> xmle. removeall (); <br/> xmld. save (server. mappath ("app_code/message. XML ")) ; // After deletion, remember to save <br/> page. clientscript. registerstartupscript (GetType (), "", "alert ('deleted successfully !! '); ", True); <br/>}< br/> else <br/>{< br/> page. clientscript. registerstartupscript (GetType (), "", "alert ('deletion failed !! '); ", True); <br/>}< br/>}

Change >>>>

Xmldocument xmld = new xmldocument (); <br/> xmld. load (server. mappath ("app_code/message. XML "); </P> <p> xmlnodelist list = xmld. selectsinglenode ("messagelist "). childnodes; </P> <p> foreach (xmlnode xmln in list) <br/>{< br/> xmlelement xmle = (xmlelement) xmln; // format conversion <br/> If (request ["ID"]. equals (xmle. getattribute ("ID") <br/>{< br/> xmle. childnodes [0]. innertext = this.txt name. value; <br/> xmle. chil Dnodes [1]. innertext = this.txt QQ. value; <br/> xmle. childnodes [2]. innertext = this.txt email. value; <br/> xmle. childnodes [3]. innertext = this.txt content. value; </P> <p> xmld. save (server. mappath ("app_code/message. XML "); <br/> page. clientscript. registerstartupscript (this. getType (), "", "alert ('modification successful !!! '); Location. href = 'messagelist. aspx' ", true); <br/>}< br/>}

Query >>>>>

// The following code is implemented by PAGE encoding <br/> xmldocument xmld = new xmldocument (); // construct an XML document <br/> xmld. load (server. mappath ("app_code/message. XML "); // load </P> <p> xmlnodelist list = xmld. selectsinglenode ("messagelist "). childnodes; // obtain the subnode <br/> If (list! = NULL) <br/>{< br/> foreach (xmlnode node in list) <br/>{< br/> xmlelement xmle = (xmlelement) node; // conversion <br/> // you can add it tomorrow.

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.