C # XML operations: write an XML record (2)

Source: Internet
Author: User
C # preliminary XML operations (5)
Chapter 2: Add records to existing XML files
In addition, C # provides a more convenient way for us to operate XML, that is, DATASET
In fact, we can think that DATASET is actually multiple XML files. Using DATASET's built-in methods and attributes to operate XML is very flexible.
Let's continue to assume that an existing 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" %>
<% @ Import Namespace = "System. Data" %>
<Script Language = "C #" Runat = "Server" Debug = "true">
Public void Page_Load (Object src, EventArgs e)
{
/*
* Copyright: copyright by SEM IT DEPARTMENT
* Version: version 0.0.1
* File: InsertXml_2.aspx
* Usage: add data to XML using DATASET
* Author: Ouyang Yuntian @ 2005-4-9
* Mail: outrace@soueast-motor.com
* Modification:
*/
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 );
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.