Десериализация c#, в документе XML (13, 11) присутствует ошибка
Формулировка задачи:
В общем, пытаюсь реализовать механизм сериализации и десериализации. Сериализация успешно выполнена, но вот в десериализации возникла проблема.
Код программы:
Где выдаёт ошибку, я скинул в скриншоте. Нужна срочно консультация по данной проблеме. Мб, я делаю что-то не так? Выручайте. Спасибо за внимание и надеюсь за исправление проблемы!
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml.Serialization;
- using System.Xml;
- using System.Security.Permissions;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public class store
- {
- public string group="";
- public string name;
- public string manufacturer;
- public int manufacturedate;
- public int shelflife;
- public store()
- {
- }
- public string _group
- {
- get
- {
- return group;
- }
- set
- {
- group = value;
- }
- }
- public string _name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public string _manufacturer
- {
- get
- {
- return manufacturer;
- }
- set
- {
- manufacturer = value;
- }
- }
- public int _manufacturedate
- {
- get
- {
- return manufacturedate;
- }
- set
- {
- if (value.ToString().Length==8)
- {
- manufacturedate = value;
- }
- else
- {
- MessageBox.Show("Количество цифр в дате изготовления должно быть равно 8!");
- }
- }
- }
- public int _shelflife
- {
- get
- {
- return shelflife;
- }
- set
- {
- if ((value==0)||(value==00)||(value == 000))
- {
- MessageBox.Show("Срок годности не может быть равен "+value+"!");
- }
- else
- {
- shelflife = value;
- }
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- int i=0;
- store[] st = new store[1];
- st[0] = new store();
- FileStream fs = new FileStream("store.xml", FileMode.Open);
- XmlSerializer xs = new XmlSerializer(typeof(store));
- XmlReader reader = XmlReader.Create(fs);
- st[0] = (store)xs.Deserialize(reader);
- while (st!=null)
- {
- dataGridView1.Rows[i].Cells[0].Value = st[0].group;
- dataGridView1.Rows[i].Cells[0].Value = st[0].name;
- dataGridView1.Rows[i].Cells[0].Value = st[0].manufacturer;
- dataGridView1.Rows[i].Cells[0].Value = st[0].manufacturedate;
- dataGridView1.Rows[i].Cells[0].Value = st[0].shelflife;
- i++;
- st[i] = (store)xs.Deserialize(reader);
- }
- //store[] st = new store[1];
- //st[] = new store();
- // Чтение
- // XmlSerializer xs = new XmlSerializer(typeof(store));
- //Stream reader = new FileStream("store.xml", FileMode.Open);
- // xs.Deserialize(reader);
- // reader.Close();
- /* int r = 0;
- int i = 0;
- StreamReader proverka = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);//для чтения файла
- if ((File.Exists("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt")) && (proverka.ReadLine() != null))
- {
- proverka.Close();
- StreamReader t = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);//для чтения файла
- while (t.ReadLine() != null)
- {
- r++;
- }
- t.Close();
- StreamReader sr = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);//для чтения файла
- store[] st = new store[r];
- for (i = 0; i < r; i++)
- {
- st[i] = new store(sr.ReadLine().Split('-'));
- }
- dataGridView1.Rows.Add(r);
- for (i = 0; i < r; i++)
- {
- dataGridView1.Rows[i].Cells[0].Value = Convert.ToString(st[i].group);
- dataGridView1.Rows[i].Cells[1].Value = Convert.ToString(st[i].name);
- dataGridView1.Rows[i].Cells[2].Value = Convert.ToString(st[i].manufacturer);
- dataGridView1.Rows[i].Cells[3].Value = Convert.ToString(st[i].manufacturedate);
- dataGridView1.Rows[i].Cells[4].Value = Convert.ToString(st[i].shelflife);
- }
- sr.Close();
- }
- else
- {
- proverka.Close();
- StreamReader proverka2 = new StreamReader("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt", System.Text.Encoding.Default);
- if (proverka2.ReadLine() == null)
- {
- proverka2.Close();
- return;
- }
- else
- {
- proverka2.Close();
- System.IO.File.Create("/практика/WindowsFormsApplication1/WindowsFormsApplication1/store.txt");
- }
- }*/
- }
- private void выходToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
- {
- int r = 0;
- Validate();//Чтобы при редактировании ячеек
- dataGridView1.Update(); //всёравно
- dataGridView1.EndEdit();// сохранялось
- r = dataGridView1.RowCount - 1;
- store[] st = new store[r];
- vvod(st, r);
- }
- public void vvod(store[] st, int r)
- {
- XmlSerializer xs = new XmlSerializer(typeof(store));
- Stream writer = new FileStream("store.xml", FileMode.Create, FileAccess.Write);
- for (int i = 0; i < r; i++)
- {
- if ((dataGridView1.Rows[i].Cells[0].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[0].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[0].Value) == " "))
- {
- writer.Close();
- MessageBox.Show("Для сохранения вся строка должна быть заполнена");
- return;
- }
- if ((dataGridView1.Rows[i].Cells[1].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[1].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[1].Value) == " "))
- {
- writer.Close();
- MessageBox.Show("Для сохранения вся строка должна быть заполнена");
- return;
- }
- if ((dataGridView1.Rows[i].Cells[2].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[2].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[2].Value) == " "))
- {
- writer.Close();
- MessageBox.Show("Для сохранения вся строка должна быть заполнена");
- return;
- }
- if ((dataGridView1.Rows[i].Cells[3].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[3].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[3].Value) == " "))
- {
- writer.Close();
- MessageBox.Show("Для сохранения вся строка должна быть заполнена");
- return;
- }
- if ((dataGridView1.Rows[i].Cells[4].Value == null) || (Convert.ToString(dataGridView1.Rows[0].Cells[4].Value) == "") || (Convert.ToString(dataGridView1.Rows[0].Cells[4].Value) == " "))
- {
- writer.Close();
- MessageBox.Show("Для сохранения вся строка должна быть заполнена");
- return;
- }
- st[i] = new store();
- st[i]._group = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
- st[i]._name = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
- st[i]._manufacturer = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
- st[i]._manufacturedate = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
- st[i]._shelflife = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
- xs.Serialize(writer, st[i]);
- }
- writer.Close();
- }
- private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
- {
- TextBox tb = (TextBox)e.Control;
- tb.KeyPress -= tb_KeyPress;
- tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
- }
- void tb_KeyPress(object sender, KeyPressEventArgs e)
- {
- string vlCell = ((TextBox)sender).Text;
- bool temp = (vlCell.IndexOf(".") == -1);
- if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].IsInEditMode == true)
- if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8) && !Char.IsWhiteSpace(e.KeyChar))
- e.Handled = true;
- if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].IsInEditMode == true)
- if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8) && !Char.IsWhiteSpace(e.KeyChar))
- e.Handled = true;
- if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].IsInEditMode == true)
- if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8) && !Char.IsWhiteSpace(e.KeyChar))
- e.Handled = true;
- if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].IsInEditMode == true)
- if (!Char.IsNumber(e.KeyChar) && (e.KeyChar != 8) || Char.IsWhiteSpace(e.KeyChar))
- e.Handled = true;
- if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].IsInEditMode == true)
- if (!Char.IsNumber(e.KeyChar) && (e.KeyChar != 8) || Char.IsWhiteSpace(e.KeyChar))
- e.Handled = true;
- }
- }
- }
Решение задачи: «Десериализация c#, в документе XML (13, 11) присутствует ошибка»
textual
Листинг программы
- var xml = new XmlSerializer(typeof(store[]));
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д