C # Operation XML Preliminary (4) Chapter 2: Add Record to the existing XML file We can add a node according to the XML structure, the property is added. In the test, we assume that the original XML file is as follows: XML Version = "1.0" Encoding = "GB2312"?>
user>
user>
users>
The following code is written by writing <% @ Import namespace = "system.io"%>
<% @ Import namespace = "system.xml"%>
Public void Page_Load (Object SRC, Eventargs E)
{
/ *
* Copyright by SEM IT Department
* Version: Version 0.0.1
* Document: INSERTXML_1.ASPX
* Use the way: Add records to XML
* Author: Ouyang Yuntian @ 2005-4-7
* Mailbox: Outrace@soueast-motor.com
* Repair:
* /
String filename;
FileName = "Users_1.xml";
Try
{
// build an XML DOM first
XmLDocument Doc = New XmLDocument ();
// Read XML file
Doc.Load (Server.mAppath ("./ files /") filename);
// Establish a node.
Xmlelement Newuser = Doc.createElement ("User");
/ / Establish a property and place the secondary attribute under contact "newuser"
Xmlattribute newid = Doc.createAttribute ("ID");
Newid.innertext = "3";
Newuser.setttributenode (NewID);
// Establish a node and use this as a child node of "newuser"
Xmlelement NewUserName = Doc.createElement ("UserName");
NewUsername.innertext = "Add User Name";
Newuser.Appendchild (newusername);
// Establish a node and use this as a child node of "newuser"
Xmlelement newuserpass = doc.createElement ("Userpass");
NewUserpass.innertext = "New Password"; newuser.appendchild (newuserpass);
// Put the node "newuser" under the root node and save
Doc.documentelement.Appendchild (newuser);
Doc.save (Server.Mappath ("./ Files /") filename); // My file is put below the File folder with the directory
}
Catch (Exception EX)
{
Response.write (ex.Message);
}
Finally
{
}
}
script>
hEAD>