Operate XML file (open, insert)

Source: Internet
Author: User

1. some specific name: xmldocument xmlnodelist xmlnode xmlelement
the whole architecture and Relation among them is following
attentions: selectsinglenodelist ("users "), obtains all direct subnodes under users. This method is often used
2. the difference between xmlnode and xmlelement
xmlelement inherits from xmlnode and has the same functions, but xmlelement has more functions
3. insert data into XML file sample:
sample 1:
xmldocument xdoc = new xmldocument ();
xdoc. load (httpcontext. current. server. mappath ("user. config ");

Xmlelement xde = xdoc. documentelement;
Xmlelement Xe = xdoc. createelement ("user ");
Xe. setattribute ("login", login );
Xe. setattribute ("password", PWD );
Xe. setattribute ("rootfolder", rootfolder );
Xe. setattribute ("rights", rights );
Xde. appendchild (xe );
Xdoc. Save (httpcontext. Current. server. mappath ("user. config "));
The XML file is the following
<? XML version = "1.0" standalone = "yes"?>
<Users>
<User Login = "sanxumei" Password = "sanxumei" rootfolder = "C: \ Inetpub \ wwroot" Rights = "user"/>
</Users>

Sample 2:
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load ("Bookstore. xml ");
Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore>
Xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> node
Xe1.setattribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
Xe1.setattribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.

Xmlelement xesub1 = xmldoc. createelement ("title ");
Xesub1.innertext = "Cs from entry to entry"; // set a text node
Xe1.appendchild (xesub1); // Add it to the <book> node
Xmlelement xesub2 = xmldoc. createelement ("author ");
Xesub2.innertext = "Hou Jie ";
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("price ");
Xesub3.innertext = "58.3 ";
Xe1.appendchild (xesub3 );

Root. appendchild (xe1); // Add it to the <bookstore> node
Xmldoc. Save ("Bookstore. xml ");

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.