Good day!

Before me is the next task - to organize the recording of data in the XML element DatagridView, and after reading there from the same file.

Writing to XML:

XmlTextWriter writer = null; writer = new XmlTextWriter("TaskList.xml", System.Text.Encoding.Unicode); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("TasksTable"); writer.WriteAttributeString("Type", "XML0.1"); writer.WriteAttributeString("Version", ""); //Запись данных writer.WriteStartElement("TaskList"); for (int i = 0; i < TasksTable.RowCount ; i++) { writer.WriteStartElement("Row"); writer.WriteAttributeString("ID", TasksTable.Rows[i].Cells[0].Value.ToString()); writer.WriteAttributeString("TaskIsDone", TasksTable.Rows[i].Cells[1].Value.ToString()); writer.WriteAttributeString("TaskIsOutOfDateDate", TasksTable.Rows[i].Cells[2].Value.ToString()); writer.WriteAttributeString("TaskCreated", TasksTable.Rows[i].Cells[3].Value.ToString()); writer.WriteAttributeString("Executor", TasksTable.Rows[i].Cells[4].Value.ToString()); writer.WriteAttributeString("TaskName", TasksTable.Rows[i].Cells[5].Value.ToString()); writer.WriteAttributeString("TaskText", TasksTable.Rows[i].Cells[6].Value.ToString()); writer.WriteAttributeString("TaskDoneDate", TasksTable.Rows[i].Cells[7].Value.ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); 

Everything is logical and understandable.

As a result, I get the file:

 <TasksTable Type="XML0.1" Version=""> <TaskList> <Row ID="1" TaskIsDone="False" TaskIsOutOfDateDate="True" TaskCreated="06.11.2012 21:57:02" Executor="1" TaskName="1" TaskText="" TaskDoneDate="06.11.2012 21:57:02"/> <Row ID="2" TaskIsDone="False" TaskIsOutOfDateDate="True" TaskCreated="06.11.2012 21:57:06" Executor="2" TaskName="2" TaskText="" TaskDoneDate="06.11.2012 21:57:06"/> <Row ID="3" TaskIsDone="False" TaskIsOutOfDateDate="True" TaskCreated="06.11.2012 21:57:09" Executor="3" TaskName="3" TaskText="" TaskDoneDate="06.11.2012 21:57:09"/> </TaskList> </TasksTable> 

(it is hard to see, but here there are three lines, as there were three lines in the e-management).

The problem is reading from this file. We determine the number of lines which should be without problems, but the values ​​themselves are not readable.

XmlTextReader reader = new XmlTextReader ("TaskList.xml"); reader.WhitespaceHandling = WhitespaceHandling.None;

  reader.ReadStartElement(); for (int i = 1; i <= reader.LineNumber; i++) { TasksTable.Rows.Add(); while (reader.Read()) { MessageBox.Show(reader.Value); //пустые значения, но кол-во сообщений равно кол-ву колонок } } 

I have never had to serialize complex objects such as a DataGridView, but I myself generally 1Snik) Can you please tell me which direction to go? Thank.

    1 answer 1

    What do you use as a data source for the grid? DataTable ? It has ready methods WriteXml and ReadXml . If you use a typed list, use the XmlSerializer class.

    Your code says that you do not understand the ideology of the work of the Dneet grids. They work with the data with which they are associated, and the API for accessing the displayed elements (rows and columns) is intended to change their display, rather than work with the data. To read and save data you need to work with the source.

    • I am calculating that the user is working with the DataGridView element on the form, and the data is stored in an XML file. If I stored the data in the database, I would do it through the DataSet, I don’t see how to attach it (and at the expense of ideology, you're right, I'm confused. I found a nice little article on serialization, from where I tried to take a piece of code for reflection ( msdn / en-ru / library / vstudio / ... ) I'll start with the ideology ... - chudo116
    • Create a DataTable required structure, assign it to the DataSource grid property and do all data manipulations via DataTable . Everything! - Modus