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:
<? Xml version = "1.0" encoding = "gb2312"?>
<Users>
<User id = "1">
<UserName> outrace </userName>
<UserPass> mypass </userPass>
</User>
<User id = "2">
<UserName> trace </userName>
<UserPass> mypass </userPass>
</User>
</Users>
Write the following code <% @ Import Namespace = "System. IO" %>
<% @ Import Namespace = "System. Xml" %>
<Script Language = "C #" Debug = "true" Runat = "Server">
Public void Page_Load (Object src, EventArgs e)
{
/*
* Copyright: copyright by SEM IT DEPARTMENT
* Version: version 0.0.1
* File: InsertXml_1.aspx
* Usage: add records to XML
* Author: Ouyang Yuntian @ 2005-4-7
* Mail: outrace@soueast-motor.com
* Modification:
*/
String fileName;
FileName = "users_1.xml ";
Try
{
// Create an xml dom first
XmlDocument doc = new XmlDocument ();
// Read the XML file
Doc. Load (Server. MapPath ("./files/") + fileName );
// Create a node.
XmlElement newUser = doc. CreateElement ("user ");
// Create an attribute and place the secondary attribute under the contact "newUser"
XmlAttribute newID = doc. CreateAttribute ("id ");
NewID. InnerText = "3 ";
NewUser. SetAttributeNode (newID );
// Create a node and use it as a subnode of "newUser"
XmlElement newUserName = doc. CreateElement ("userName ");
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.