C # XML operations 3

Source: Internet
Author: User
Chapter 3: Edit XML file records
Use C # To edit XML files. The most convenient method is to use DATASET.
We continue to use the XML file in the previous article, as shown below:<Users> <br/> <Xs: schema id = "users" xmlns = "" xmlns: xs = "http://www.w3.org/2001/XMLSchema" xmlns: msdata = "urn: Schemas-Microsoft-com: XML-msdata "> <br/> <Xs: element name =" users "msdata: isdataset =" true "msdata: locale = "ZH-CN"> <br/> <Xs: complextype> <br/> <Xs: Choice maxoccurs = "unbounded"> <br/> <Xs: element name = "user"> <br/> <Xs: complextype> <br/> <Xs: sequence> <br/> <Xs: element name = "username" type = "XS: string" minoccurs = "0" msdata: ordinal = "0"/> <br/> <Xs: element name = "userpass" type = "XS: string" minoccurs = "0" msdata: ordinal = "1"/> <br/> </Xs: sequence> <br/> <Xs: attribute name = "ID" type = "XS: string"/> <br/> </Xs: complextype> <br/> </Xs: Element> <br/> </Xs: Choice> <br/> </Xs: complextype> <br/> </Xs: element> <br/> </Xs: schema> <br/> <user id = "1"> <br/> <username> outrace </username> <br/> <userpass> mypass </userpass> <br /> </user> <br/> <user id = "2"> <br/> <username> trace </username> <br/> <userpass> mypass </ userpass> <br/> </user> <br/> <user id = "3"> <br/> <username> New User </username> <br/> <userpass> New passwd </userpass> <br/> </user> <br/> </users> <br/>
Then, we create an XML file with the following content:<% @ Import namespace = "system. io "%> <br/> <% @ import namespace =" system. XML "%> <br/> <% @ import namespace =" system. data "%> <br/> <script language =" C # "runat =" server "> <br/> Public void page_load (Object SRC, eventargs E) <br/>{< br/>/* <br/> * copyright: copyright by sem it department <br/> * version: version 0.0.1 <br/> * file: editxml. aspx <br/> *: Edit XML files (modify and delete rows) <br/> * Prepared by: Ouyang yuntian @ 2005-4-9 <br/> * mail box: outrace@soueast-motor.com <br/> * repair: <br/> */</P> <p> string filename; <br/> filename = "users_2.xml "; <br/> dataset myds = new dataset (); </P> <p> try <br/> {<br/> filestream fin; <br/> fin = new filestream (server. mappath (". /files/"+ filename), filemode. open, fileaccess. read, fileshare. readwrite); <br/> myds. readxml (FIN); <br/> fin. close (); <br/>}< br/> catch (exception ex) <br/>{< br/> response. write (ex. message); <br/>}</P> <p> // bind the modified XML <br/> olddata. datasource = myds. tables [0]. defaultview; <br/> olddata. databind (); </P> <p> // when a new row is added to DS <br/> datarow newrow = myds. tables [0]. newrow (); <br/> newrow ["ID"] = "44"; <br/> newrow ["username"] = "test user "; <br/> newrow ["userpass"] = "test passwd"; <br/> myds. tables [0]. rows. add (newrow); </P> <p> // bind the data of a new record. <br/> newdata. datasource = myds. tables [0]. defaultview; <br/> newdata. databind (); </P> <p> // filter <br/> datarow [] editrow = myds. tables [0]. select ("ID = '44'"); </P> <p> for (INT I = 0; I <editrow. length; I ++) <br/>{< br/> editrow [I] ["username"] = "changed user "; <br/> editrow [I] ["userpass"] = "changed passwd "; <br/>}</P> <p> // bind the modified data record. <br/> editdata. datasource = myds. tables [0]. defaultview; <br/> editdata. databind (); </P> <p> for (INT I = 0; I <editrow. length; I ++) <br/>{< br/> editrow [I]. delete (); // Delete the data <br/>}</P> <p> // bind the data of the deleted record <br/> deledata. datasource = myds. tables [0]. defaultview; <br/> deledata. databind (); </P> <p> // write changes to myds into XML <br/> try <br/>{< br/> myds. writexml (server. mappath (". /files/"+ filename), xmlwritemode. writeschema); <br/>}< br/> catch (exception ex) <br/>{< br/> response. write (ex. message); <br/>}</P> <p> myds. dispose (); <br/>}< br/> </SCRIPT> </P> <p> <HTML> </P> <p> <pead> <br/> <title> edit data </title> <br/> </pead> </P> <p> <body> <br/> <form runat = "server"> <br/> <Table width = "100%"> <br/> <tr> <br/> <TD> original data </TD> <br/> <TD> Add row </TD> <br/> <TD> modify a row </TD> <br/> <TD> delete a row </TD> <br/> </tr> <br/> <tr> <br/> <TD valign = "TOP"> <asp: dataGrid id = "olddata" runat = "server"/> </TD> <br/> <TD valign = "TOP"> <asp: dataGrid id = "newdata" runat = "server"/> </TD> <br/> <TD valign = "TOP"> <asp: dataGrid id = "editdata" runat = "server"/> </TD> <br/> <TD valign = "TOP"> <asp: dataGrid id = "deledata" runat = "server"/> </TD> <br/> </tr> <br/> </table> <br/> </Form> <br/> </body> </P> <p> </ptml> <br/>
After running this file, you can see the operation result intuitively. Because we saved the file at the end, the content of the XML file has not changed.
Related Article

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.