【问题标题】:Appending in an existing xml file using dataset使用数据集附加到现有的 xml 文件中
【发布时间】:2015-02-19 22:29:40
【问题描述】:

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

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

你可以看到 user1 被覆盖了 无论如何,这是我的代码:

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..");
    }
}

【问题讨论】:

    标签: c# xml winforms dataset


    【解决方案1】:

    您需要使用 'ds.Merge(dt);'像下面的例子:

    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文件

    <?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>
    

    如果有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-12
      • 2012-11-24
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多