简体   繁体   中英

“Multiple” XML-File (new input should not replace) using c#

I'm writing a short program which displays me all my To Do's. I got 2 tabs. 1 Tab is for create a new To Do and the other tab is for displaying me the details of every To Do I selected. So I have to save the created To Do's in a xml file. For this I am using DataTable and DataSet and it works fine. Here my code:

DataTable datatable = new DataTable();
            datatable.TableName = "SaveInput";

            DataColumn dc1 = new DataColumn("Name");
            DataColumn dc2 = new DataColumn("Priority");
            DataColumn dc3 = new DataColumn("StartDate");
            DataColumn dc4 = new DataColumn("EndDateSoll");
            DataColumn dc5 = new DataColumn("EndDateIst");
            DataColumn dc6 = new DataColumn("Comment");

            datatable.Columns.Add(dc1);
            datatable.Columns.Add(dc2);
            datatable.Columns.Add(dc3);
            datatable.Columns.Add(dc4);
            datatable.Columns.Add(dc5);
            datatable.Columns.Add(dc6);

            datatable.Rows.Add(txt_Name.Text, combox_Priority.Text, txt_Beginn.Text, txt_EndSoll.Text, txt_EndIst.Text, txt_Bemerkungen.Text);

            DataSet dataset = new DataSet();

            dataset.Tables.Add(datatable);
            dataset.DataSetName = "MyProgram";

            dataset.WriteXml(@"C:\Users\rs\Desktop\Test\save.xml");

But my problem now is: If I create a new "To Do" in tab1, it replace the new "To Do" in the .xml file. So if I have a look at the .xml file, there is still 1 "To Do". I just want to write more tables in the .xml file. So my .xml file looks like this:

<MyProgram>
  <SaveInput>
    <Name>todo1</Name>
    <Priority>high</Priority>
    <StartDate>today</StartDate>
    <EndDateSoll>later</EndDateSoll>
    <EndDateIst>not finished</EndDateIst>
    <Comment>blabla</Comment>
  </SaveInput>
</MyProgram>

But it should look like this for example:

<MyProgram>
  <SaveInput>
    <Name>todo1</Name>
    <Priority>high</Priority>
    <StartDate>today</StartDate>
    <EndDateSoll>later</EndDateSoll>
    <EndDateIst>not finished</EndDateIst>
    <Comment>blabla</Comment>

    <Name>todo2</Name>
    <Priority>high</Priority>
    <StartDate>yesterday</StartDate>
    <EndDateSoll>tomorrow</EndDateSoll>
    <EndDateIst>not finished</EndDateIst>
    <Comment>testtest</Comment>
  </SaveInput>
</MyProgram>

I hope someone can help me or give me a hint so I can create more than 1 To-Do.

Cheers

EDIT: It's a WinForm Application

This code should work for you.

Check it out

DataSet ds = new DataSet();
            if (File.Exists(@"C:\Users\rs\Desktop\Test\save.xml"))
            {
                ds.ReadXml(@"C:\Users\rs\Desktop\Test\save.xml");

                ds.Tables[0].Rows.Add(txt_Name.Text, combox_Priority.Text, txt_Beginn.Text, txt_EndSoll.Text, txt_EndIst.Text, txt_Bemerkungen.Text);
                ds.WriteXml(@"C:\Users\rs\Desktop\Test\save.xml");
            }
            else
            {
                DataTable datatable = new DataTable();
                datatable.TableName = "SaveInput";

                DataColumn dc1 = new DataColumn("Name");
                DataColumn dc2 = new DataColumn("Priority");
                DataColumn dc3 = new DataColumn("StartDate");
                DataColumn dc4 = new DataColumn("EndDateSoll");
                DataColumn dc5 = new DataColumn("EndDateIst");
                DataColumn dc6 = new DataColumn("Comment");

                datatable.Columns.Add(dc1);
                datatable.Columns.Add(dc2);
                datatable.Columns.Add(dc3);
                datatable.Columns.Add(dc4);
                datatable.Columns.Add(dc5);
                datatable.Columns.Add(dc6);

                datatable.Rows.Add(txt_Name.Text, combox_Priority.Text, txt_Beginn.Text, txt_EndSoll.Text, txt_EndIst.Text, txt_Bemerkungen.Text);

                DataSet dataset = new DataSet();

                dataset.Tables.Add(datatable);
                dataset.DataSetName = "MyProgram";

                dataset.WriteXml(@"C:\Users\rs\Desktop\Test\save.xml");
            }

Correct me if I'm wrong, but from your code it seems to me that you always create a new dataset, that contains only the new values. Do you read the already existing Todos first? If not, you simply always overwrite your xml file and the previous entries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM