Does not work. I add the data to the textbox. I press the button. Data is saved. I open xml, and in it only the last value. Where is the error, tell me?

private void button12_Click(object sender, EventArgs e) // НЕИЗВЕСТНО РАБОТАЕТ { // таблица DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.TableName = "SearchWords"; // название таблицы dt.Columns.Add("word"); // название колонки ds.Tables.Add(dt); // добавляем строки DataRow row = ds.Tables["SearchWords"].NewRow(); row["word"] = textBox4.Text; ds.Tables["SearchWords"].Rows.Add(row); // сохраняем ds.WriteXml("D:\\Words.xml"); } 

The xml only needs one column with the word data entered.

 <?xml version="1.0" standalone="yes"?> <NewDataSet> <SearchWords> <word>111111111</word> </SearchWords> </NewDataSet> 

And tell me how to make xml like this:

 <NewDataSet> <SearchWords> <word>888888</word> <word>22</word> <word>11111</word> </SearchWords> </NewDataSet> 

And with the current code it turns out like this (i.e. a lot of extra tags):

 <?xml version="1.0" standalone="yes"?> <NewDataSet> <SearchWords> <word>888888</word> </SearchWords> <SearchWords> <word>22</word> </SearchWords> <SearchWords> <word>11111</word> </SearchWords> </NewDataSet> 
  • one
    To get the desired xml, you have to drop the DataSet / DataTable. Use, for example, LinqToXml. But this is a topic for another question, it is better to ask it separately. - Alexander Petrov

1 answer 1

All objects are new, created in button12_Click . How do they find out about the current content of "D: \ Words.xml"?

 private void button12_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); if (System.IO.File.Exists("D:\\Words.xml")) { ds.ReadXml("D:\\Words.xml"); } else { DataTable dt = new DataTable(); dt.TableName = "SearchWords"; // название таблицы dt.Columns.Add("word"); // название колонки ds.Tables.Add(dt); } // добавляем строки ... } 
  • Do you want an explanation why it works "for a Hindu on video"? I do not know a Hindu and have not seen the video. I explained why the code you provided does what it does. - Igor