C # Operation XML Preliminary (5) Chapter 2: Add a record to the existing XML file to add another C # and provide a more convenient way to operate XML, that is, the Dataset is actually what we think that Dataset is actually a plurality of XML files, With some built-in methods and attributes to operate XML, very flexible, we continue to assume, existing XML files, 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"%>
<% @ Import namespace = "system.data"%>
Public void Page_Load (Object SRC, Eventargs E)
{
/ *
* Copyright by SEM IT Department
* Version: Version 0.0.1
* Document: INSERTXML_2.ASPX
* Use the way: add data to XML using DataSet
* Author: Ouyang Yuntian @ 2005-4-9
* Mailbox: Outrace@soueast-motor.com
* Repair:
* /
String filename;
FileName = "Users_2.xml";
DataSet myds = new dataset ();
Try
{
FileStream Fin;
Fin = new filestream (server.mappath ("./ files /" filename), filemode.open, fileaccess.read, fileshare.readwrite;
Myds.readxml (FIN);
Fin.close ();
}
Catch (Exception EX)
{
Response.write (ex.Message);
}
/ / Bind XML before modification
Predata.datasource = myds.tables [0] .defaultView; // Specify data source
Predata.DATABIND (); // Binding data
// When the DS is added to the new line
/ / Here we can see that in fact the properties of the original XML data, we can use the DataRow [] array directly, not just a node.
DataRow newrow = myds.tables [0] .newrow (); newrow ["id"] = "3";
NEWROW ["UserName"] = "new user";
NEWROW ["Userpass"] = "new passwd";
MYDS.TABLES [0]. Rows.Add (newrow);
// write MYDS to XML
Try
{
FileStream Data;
Dataout = new filestream (server.mappath ("./ files /" filename), filemode.open, fileaccess.write, fileshare.readwrite;
Myds.writexml (Data, XMLWRITE.WRITESCHEMA);
Dataout.close ();
}
Catch (Exception EX)
{
Response.write (ex.Message);
}
// Bind the modified XML
NextData.DataSource = myds.tables [0] .defaultView; // Specify data source
NextData.DATABIND (); // Binding data
myds.dispose ();
}
script>
hEAD>