使用数据集追加到现有的xml文件中 [英] Appending in an existing xml file using dataset

查看:108
本文介绍了使用数据集追加到现有的xml文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这段代码,但是它有一个问题,每次我运行它..it都会覆盖我的xml文件,并且没有添加任何新内容
这是我的xml文件:

I made this code but it has a problem that everytime i run it ..it overwrites my xml file and nothing new is added this is my xml file:

- <DATA>
- <Users>
  <MonopolyID>User2</MonopolyID> 
  <Password>pass2</Password> 
  <FirstName>User2Name</FirstName> 
  <LastName>User2LastName</LastName> 
  </Users>
  </DATA>

u仍然可以看到user1被
覆盖了,这是我的代码:

u can see that user1 was overwritten anyway this is my code:

public partial class SignUpPage : Form
    {
private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("MonopolyID", typeof(string));
            dt.Columns.Add(dc);
            dc = new DataColumn("Password", typeof(string));
            dt.Columns.Add(dc);
            dc = new DataColumn("FirstName", typeof(string));
            dt.Columns.Add(dc);
            dc = new DataColumn("LastName", typeof(string));
            dt.Columns.Add(dc);
            DataRow dr = dt.NewRow();
            dt.Rows.Add(textBox3.Text, textBox4.Text, textBox1.Text, textBox2.Text);//here just putting  the texts in the texts box for the first row
            dt.TableName = "Users";
            ds.Tables.Add(dt);
            ds.DataSetName = "DATA";
            ds.WriteXml(@"pathOfTheFile..");
    }
}


推荐答案

您需要使用'ds.Merge(dt);',如下例所示:

You need to use 'ds.Merge(dt);' like below example:

protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/xmldata.xml"));
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("MonopolyID", typeof(string));
        dt.Columns.Add(dc);
        dc = new DataColumn("Password", typeof(string));
        dt.Columns.Add(dc);
        dc = new DataColumn("FirstName", typeof(string));
        dt.Columns.Add(dc);
        dc = new DataColumn("LastName", typeof(string));
        dt.Columns.Add(dc);
        DataRow dr = dt.NewRow();
        dt.Rows.Add("User3", "pass3", "User3Name", "User3LastName");
        //dt.TableName = "Users";
        ds.Tables.Add(dt);
        //ds.DataSetName = "DATA";
        ds.Merge(dt);
        ds.WriteXml(Server.MapPath("~/xmldata.xml"));
    }

到目前为止,在xml文件下生成的

And so far generated below xml file

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table1>
    <MonopolyID>User1</MonopolyID>
    <Password>pass1</Password>
    <FirstName>User1Name</FirstName>
    <LastName>User1LastName</LastName>
  </Table1>
  <Table2>
    <MonopolyID>User2</MonopolyID>
    <Password>pass2</Password>
    <FirstName>User2Name</FirstName>
    <LastName>User2LastName</LastName>
  </Table2>
  <Table3>
    <MonopolyID>User3</MonopolyID>
    <Password>pass3</Password>
    <FirstName>User3Name</FirstName>
    <LastName>User3LastName</LastName>
  </Table3>
</NewDataSet>

让我知道是否有帮助。

这篇关于使用数据集追加到现有的xml文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆