且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用数据集追加到现有的xml文件中

更新时间:2022-05-24 00:26:34

您需要使用'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>

让我知道是否有帮助。