There is such a database. I add several fields to the DGV, and then I need an event (Import in XML) in the handler to import all the data that was in the DGV into an XML file. I inserted the serialization code into the Add button, but then only one record could be saved in the XML that I added the first. And I need to first add a few to the grid, and then everything is in XML.

DB

////кнопка add private void button1_Click(object sender, EventArgs e) { Form2 friendForm = new Form2(); DialogResult result = friendForm.ShowDialog(this); if (result == DialogResult.Cancel) return; friends friends = new friends(); friends.FirstName = friendForm.textBox1.Text; friends.LastName = friendForm.textBox2.Text; friends.Age = (int)friendForm.numericUpDown1.Value; friends.Number = friendForm.textBox3.Text; friends.House = (int)friendForm.numericUpDown2.Value; friends.Adress = friendForm.textBox4.Text; db.friendsSet.Add(friends); db.SaveChanges(); XmlSerializer serial = new XmlSerializer(typeof(friends)); using (FileStream fs = new FileStream(Environment.CurrentDirectory + "\\book.xml", FileMode.Create, FileAccess.Write)) { serial.Serialize(fs, friends); MessageBox.Show("XML was created"); } } 
  • Mmm ... Actually, it should be "Export to XML", "Import from XML". Export somewhere, Import from somewhere. - Alexander Petrov

1 answer 1

To begin with, you should think: is it necessary to duplicate the data in the xml-file, if they are already stored in the database? The database is designed to ensure that data can be easily added, deleted, modified, searched. If for some purposes you still need data in xml format, you can always get it directly from the database. One feature is the FOR XML command.


Answer directly to your question. Get a list of Friend objects (rename the class, Visual Studio will automatically make changes in the whole project).

 List<Friend> _friends = new List<Friend>(); 

This must be the field of the form where serialization is performed.

After receiving another instance of this class, add it to this collection:

 Friend friend = new Friend(); friend.FirstName = friendForm.textBox1.Text; ... db.friendsSet.Add(friend); db.SaveChanges(); _friends.Add(friend); // добавляем в список 

When the entry of all data is completed, we serialize the entire list:

 XmlSerializer serial = new XmlSerializer(typeof(List<Friend>)); // <-- using (FileStream fs = new FileStream(Environment.CurrentDirectory + "\\book.xml", FileMode.Create, FileAccess.Write)) { serial.Serialize(fs, _friends); // <-- MessageBox.Show("XML was created"); } 

Judging by the screenshot, this code should be placed in the handler of the "Export" button. By the way, call db.SaveChanges(); I would transfer there too.

Note: the serializer must be created of the appropriate type; at the entrance he serves not one copy, but a collection.

To deserialize, respectively, you need a collection by casting to List<Friend> .

  • Thank you very much) but now the error "Friends type of entity is not included in the model for the current context" during the compilation process - Oleksandr Pavlyshyn
  • @OleksandrPavlyshyn - If you do not know how to correct the context, then do not rename the class. - Alexander Petrov
  • oh fool, I’ve done everything and the context and everything works as it should, thanks a lot for what I would do without you) - Oleksandr Pavlyshyn