Десериализация c#, в документе XML (13, 11) присутствует ошибка

Узнай цену своей работы

Формулировка задачи:

В общем, пытаюсь реализовать механизм сериализации и десериализации. Сериализация успешно выполнена, но вот в десериализации возникла проблема. Код программы:
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Xml.Serialization;
  12. using System.Xml;
  13. using System.Security.Permissions;
  14. namespace WindowsFormsApplication1
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. public class store
  23. {
  24. public string group="";
  25. public string name;
  26. public string manufacturer;
  27. public int manufacturedate;
  28. public int shelflife;
  29. public store()
  30. {
  31. }
  32. public string _group
  33. {
  34. get
  35. {
  36. return group;
  37. }
  38. set
  39. {
  40. group = value;
  41. }
  42. }
  43. public string _name
  44. {
  45. get
  46. {
  47. return name;
  48. }
  49. set
  50. {
  51. name = value;
  52. }
  53. }
  54. public string _manufacturer
  55. {
  56. get
  57. {
  58. return manufacturer;
  59. }
  60. set
  61. {
  62. manufacturer = value;
  63. }
  64. }
  65. public int _manufacturedate
  66. {
  67. get
  68. {
  69. return manufacturedate;
  70. }
  71. set
  72. {
  73. if (value.ToString().Length==8)
  74. {
  75. manufacturedate = value;
  76. }
  77. else
  78. {
  79. MessageBox.Show("Количество цифр в дате изготовления должно быть равно 8!");
  80. }
  81. }
  82. }
  83. public int _shelflife
  84. {
  85. get
  86. {
  87. return shelflife;
  88. }
  89. set
  90. {
  91. if ((value==0)||(value==00)||(value == 000))
  92. {
  93. MessageBox.Show("Срок годности не может быть равен "+value+"!");
  94. }
  95. else
  96. {
  97. shelflife = value;
  98. }
  99. }
  100. }
  101. }
  102. private void Form1_Load(object sender, EventArgs e)
  103. {
  104. int i=0;
  105. store[] st = new store[1];
  106. st[0] = new store();
  107. FileStream fs = new FileStream("store.xml", FileMode.Open);
  108. XmlSerializer xs = new XmlSerializer(typeof(store));
  109. XmlReader reader = XmlReader.Create(fs);
  110. st[0] = (store)xs.Deserialize(reader);
  111. while (st!=null)
  112. {
  113. dataGridView1.Rows[i].Cells[0].Value = st[0].group;
  114. dataGridView1.Rows[i].Cells[0].Value = st[0].name;
  115. dataGridView1.Rows[i].Cells[0].Value = st[0].manufacturer;
  116. dataGridView1.Rows[i].Cells[0].Value = st[0].manufacturedate;
  117. dataGridView1.Rows[i].Cells[0].Value = st[0].shelflife;
  118. i++;
  119. st[i] = (store)xs.Deserialize(reader);
  120. }
  121.  
  122. //store[] st = new store[1];
  123. //st[] = new store();
  124. // Чтение
  125. // XmlSerializer xs = new XmlSerializer(typeof(store));
  126. //Stream reader = new FileStream("store.xml", FileMode.Open);
  127. // xs.Deserialize(reader);
  128. // reader.Close();
  129. /* int r = 0;
  130. int i = 0;
  131. StreamReader proverka = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);//для чтения файла
  132. if ((File.Exists("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt")) && (proverka.ReadLine() != null))
  133. {
  134. proverka.Close();
  135. StreamReader t = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);//для чтения файла
  136. while (t.ReadLine() != null)
  137. {
  138. r++;
  139. }
  140. t.Close();
  141. StreamReader sr = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);//для чтения файла
  142. store[] st = new store[r];
  143. for (i = 0; i < r; i++)
  144. {
  145. st[i] = new store(sr.ReadLine().Split('-'));
  146. }
  147. dataGridView1.Rows.Add(r);
  148. for (i = 0; i < r; i++)
  149. {
  150. dataGridView1.Rows[i].Cells[0].Value = Convert.ToString(st[i].group);
  151. dataGridView1.Rows[i].Cells[1].Value = Convert.ToString(st[i].name);
  152. dataGridView1.Rows[i].Cells[2].Value = Convert.ToString(st[i].manufacturer);
  153. dataGridView1.Rows[i].Cells[3].Value = Convert.ToString(st[i].manufacturedate);
  154. dataGridView1.Rows[i].Cells[4].Value = Convert.ToString(st[i].shelflife);
  155. }
  156. sr.Close();
  157. }
  158. else
  159. {
  160. proverka.Close();
  161. StreamReader proverka2 = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);
  162. if (proverka2.ReadLine() == null)
  163. {
  164. proverka2.Close();
  165. return;
  166. }
  167. else
  168. {
  169. proverka2.Close();
  170. System.IO.File.Create("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt");
  171. }
  172. }*/
  173. }
  174. private void выходToolStripMenuItem_Click(object sender, EventArgs e)
  175. {
  176. this.Close();
  177. }
  178. private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
  179. {
  180. int r = 0;
  181. Validate();//Чтобы при редактировании ячеек
  182. dataGridView1.Update(); //всёравно
  183. dataGridView1.EndEdit();// сохранялось
  184. r = dataGridView1.RowCount - 1;
  185. store[] st = new store[r];
  186. vvod(st, r);
  187. }
  188. public void vvod(store[] st, int r)
  189. {
  190. XmlSerializer xs = new XmlSerializer(typeof(store));
  191. Stream writer = new FileStream("store.xml", FileMode.Create, FileAccess.Write);
  192. for (int i = 0; i < r; i++)
  193. {
  194. if ((dataGridView1.Rows[i].Cells[0].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[0].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[0].Value) == " "))
  195. {
  196. writer.Close();
  197. MessageBox.Show("Для сохранения вся строка должна быть заполнена");
  198. return;
  199. }
  200. if ((dataGridView1.Rows[i].Cells[1].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[1].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[1].Value) == " "))
  201. {
  202. writer.Close();
  203. MessageBox.Show("Для сохранения вся строка должна быть заполнена");
  204. return;
  205. }
  206. if ((dataGridView1.Rows[i].Cells[2].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[2].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[2].Value) == " "))
  207. {
  208. writer.Close();
  209. MessageBox.Show("Для сохранения вся строка должна быть заполнена");
  210. return;
  211. }
  212. if ((dataGridView1.Rows[i].Cells[3].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[3].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[3].Value) == " "))
  213. {
  214. writer.Close();
  215. MessageBox.Show("Для сохранения вся строка должна быть заполнена");
  216. return;
  217. }
  218. if ((dataGridView1.Rows[i].Cells[4].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[4].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[4].Value) == " "))
  219. {
  220. writer.Close();
  221. MessageBox.Show("Для сохранения вся строка должна быть заполнена");
  222. return;
  223. }
  224. st[i] = new store();
  225. st[i]._group = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
  226. st[i]._name = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
  227. st[i]._manufacturer = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
  228. st[i]._manufacturedate = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
  229. st[i]._shelflife = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
  230. xs.Serialize(writer, st[i]);
  231. }
  232. writer.Close();
  233. }
  234. private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  235. {
  236. TextBox tb = (TextBox)e.Control;
  237. tb.KeyPress -= tb_KeyPress;
  238. tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
  239. }
  240. void tb_KeyPress(object sender, KeyPressEventArgs e)
  241. {
  242. string vlCell = ((TextBox)sender).Text;
  243. bool temp = (vlCell.IndexOf(".") == -1);
  244. if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].IsInEditMode == true)
  245. if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8) && !Char.IsWhiteSpace(e.KeyChar))
  246. e.Handled = true;
  247. if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].IsInEditMode == true)
  248. if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8) && !Char.IsWhiteSpace(e.KeyChar))
  249. e.Handled = true;
  250. if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].IsInEditMode == true)
  251. if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8) && !Char.IsWhiteSpace(e.KeyChar))
  252. e.Handled = true;
  253. if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].IsInEditMode == true)
  254. if (!Char.IsNumber(e.KeyChar) && (e.KeyChar != 8) || Char.IsWhiteSpace(e.KeyChar))
  255. e.Handled = true;
  256. if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].IsInEditMode == true)
  257. if (!Char.IsNumber(e.KeyChar) && (e.KeyChar != 8) || Char.IsWhiteSpace(e.KeyChar))
  258. e.Handled = true;
  259. }
  260. }
  261. }
Где выдаёт ошибку, я скинул в скриншоте. Нужна срочно консультация по данной проблеме. Мб, я делаю что-то не так? Выручайте. Спасибо за внимание и надеюсь за исправление проблемы!

Решение задачи: «Десериализация c#, в документе XML (13, 11) присутствует ошибка»

textual
Листинг программы
  1. var xml = new XmlSerializer(typeof(store[]));

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 3.714 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут