C # xml operations 1

Source: Internet
Author: User
Chapter 2: Add records to existing XML files
You can add nodes and attributes according to the XML structure.
In the test, we assume that the original XML file is as follows:
<textarea class="code" name="code" rows="10" cols="80"><? XML version = "1.0" encoding = "gb2312"?> <Br/> <users> <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/> </users> <br/></textarea>
The following Code Write operations<% @ Import namespace = "system. io "%> <br/> <% @ import namespace =" system. XML "%> <br/> <script language =" C # "DEBUG =" true "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: insertxml_1.aspx <br/> * usage: Add a record to XML <br/> * Author: ouyang yuntian @ 2005-4-7 <br/> * mailbox: outrace@soueast-motor.com <br/> * Modify: <br/> */</P> <p> string filename; </P> <p> filename = "users_1.xml "; </P> <p> try <br/> {<br/> // create an xml dom <br/> xmldocument Doc = new xmldocument (); </P> <p> // read the XML file <br/> Doc. load (server. mappath (". /files/") + filename); </P> <p> // create a node. <Br/> xmlelement newuser = Doc. createelement ("user"); </P> <p> // create an attribute and place the Secondary attribute under the contact "newuser" <br/> xmlattribute newid = Doc. createattribute ("ID"); <br/> newid. innertext = "3"; <br/> newuser. setattributenode (newid); </P> <p> // create a node and use it as a subnode of "newuser" <br/> xmlelement newusername = Doc. createelement ("username"); <br/> newusername. innertext = "New User Name"; <br/> newuser. appendchild (newusername); </P> <p> // create a node and use it as a subnode of "newuser" <br/> xmlelement newuserpass = Doc. createelement ("userpass"); <br/> newuserpass. innertext = "new password"; <br/> newuser. appendchild (newuserpass); </P> <p> // place the node "newuser" under the root node and save it <br/> Doc. documentelement. appendchild (newuser); <br/> Doc. save (server. mappath (". /files/") + filename); // my files are stored in the file folder in the same directory <br/>}< br/> catch (exception ex) <br/> {<br/> response. write (ex. message ); <br/>}< br/> finally <br/>{< br/>}< br/> </SCRIPT> </P> <p> <HTML> </P> <p> <pead> <br/> <title> insert a data entry </title> <br/> </pead> </P> <p> <body> <br/> <form runat = "server"> <br/> <B> the XML data is successfully written, file Content: </B> <br/> <p> <asp: XML id = "myuser" documentsource = ". /files/users_1.xml "runat =" server "/> </P> <br/> </form> <br/> </body> </P> <p> </ptml> <br/>
through the above operation, we will get the following XML results:
<? XML version = "1.0" encoding = "gb2312"?> <Br/> <users> <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 Name </username> <br/> <userpass> New Password </userpass> <br/> </user> <br/> </users> <br/>
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.