Десериализация XML не срабатывает - C#
Формулировка задачи:
Здравствуйте. Столкнулась с такой проблемой: неделю назад благополучно решила вопрос с записью объектов в XML и обратно методами сериализации/десериализации. Всё отлично работало. Сегодня работать перестало, хз почему, ничего не меняла. Сериализация работает - данные из List<> прекрасно записывает в XML. Но обратно из XML в List<> ни в какую. Причем ошибки никакой нет, просто упорно пишет, что в List<> 0 записей, хотя должно быть не 0.
В чем может быть проблема? (компьютер перезагружала)
Класс
Сериализация:
Инициализация l_vishistpeople:
Десериализация:
Дозапись в XML файл:
public class VisitHistoryPeople { int id_visitpeople; public int Id_visitpeople { get { return id_visitpeople; } set { id_visitpeople = value; } } int id_people; public int Id_people { get { return id_people; } set { id_people = value; } } int id_request; public int Id_request { get { return id_request; } set { id_request = value; } } int from; public int From { get { return from; } set { from = value; } } int where; public int Where { get { return where; } set { where = value; } } DateTime time; public DateTime Time { get { return time; } set { time = value; } } public VisitHistoryPeople() { } public VisitHistoryPeople(int id_visit, int id_people, int id_request, int f, int w, DateTime t) { Id_visitpeople = id_visit; Id_people = id_people; Id_request = id_request; From = f; Where = w; Time = t; } public VisitHistoryPeople(int id_people, int id_request, int f, int w, DateTime t) { Id_people = id_people; Id_request = id_request; From = f; Where = w; Time = t; } }
public void WriteVisitHistoryPeopleToXML() { FileStream stream = new FileStream(pathVisitHistoryPeople, FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(List<_Client.ServiceReference1.VisitHistoryPeople>)); serializer.Serialize(stream, l_vishistpeople); stream.Close(); }
List<_Client.ServiceReference1.VisitHistoryPeople> = l_vishistpeople = PutXmlIntoListVisitHistoryPeople();
public List<_Client.ServiceReference1.VisitHistoryPeople> PutXmlIntoListVisitHistoryPeople() { XmlSerializer reader = new XmlSerializer(typeof(List<_Client.ServiceReference1.VisitHistoryPeople>)); StreamReader file = new StreamReader(pathVisitHistoryPeople); List<_Client.ServiceReference1.VisitHistoryPeople> lvhp = (List<_Client.ServiceReference1.VisitHistoryPeople>)reader.Deserialize(file); file.Close(); return lvhp; }
public void AddItemToVisitHistory(_Client.ServiceReference1.VisitHistoryPeople vhp) { XmlDocument _doc = new XmlDocument(); _doc.Load(pathVisitHistoryPeople); XmlElement newVisitHistoryPeople = _doc.CreateElement("VisitHistoryPeople"); XmlElement newIdVHP = _doc.CreateElement("Id_visitpeople"); newIdVHP.InnerText = "0"; newVisitHistoryPeople.AppendChild(newIdVHP); XmlElement newIdP = _doc.CreateElement("Id_people"); newIdP.InnerText = Convert.ToString(vhp.Id_people); newVisitHistoryPeople.AppendChild(newIdP); XmlElement newIdR = _doc.CreateElement("Id_request"); newIdR.InnerText = Convert.ToString(vhp.Id_request); newVisitHistoryPeople.AppendChild(newIdR); XmlElement newIdF = _doc.CreateElement("From"); newIdF.InnerText = Convert.ToString(vhp.From); newVisitHistoryPeople.AppendChild(newIdF); XmlElement newIdW = _doc.CreateElement("Where"); newIdW.InnerText = Convert.ToString(vhp.Where); newVisitHistoryPeople.AppendChild(newIdW); XmlElement newIdT = _doc.CreateElement("Time"); newIdT.InnerText = XmlConvert.ToString(vhp.Time, XmlDateTimeSerializationMode.Unspecified); newVisitHistoryPeople.AppendChild(newIdT); _doc.DocumentElement.AppendChild(newVisitHistoryPeople); _doc.Save(pathVisitHistoryPeople); }
Решение задачи: «Десериализация XML не срабатывает»
textual
Листинг программы
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; namespace Marronie { class Program { static void Main(string[] args) { List<VisitHistoryPeople> l1 = new List<VisitHistoryPeople>(); l1.Add(new VisitHistoryPeople(1, 1, 1, 1, DateTime.Now)); l1.Add(new VisitHistoryPeople(1, 1, 1, 1, DateTime.Now)); VisitHistoryPeople.Save(l1, "test.xml"); var a = VisitHistoryPeople.Load("test.xml"); } private static List<VisitHistoryPeople> l_vishistpeople; private static string pathVisitHistoryPeople = "doc.xml"; } public class VisitHistoryPeople { public int Id_visitpeople { get; set; } public int Id_people { get; set; } public int Id_request { get; set; } public int From { get; set; } public int Where { get; set; } public DateTime Time { get; set; } public VisitHistoryPeople() { } public VisitHistoryPeople(int id_visit, int id_people, int id_request, int f, int w, DateTime t) { Id_visitpeople = id_visit; Id_people = id_people; Id_request = id_request; From = f; Where = w; Time = t; } public VisitHistoryPeople(int id_people, int id_request, int f, int w, DateTime t) { Id_people = id_people; Id_request = id_request; From = f; Where = w; Time = t; } public static void Save(List<VisitHistoryPeople> List, string FileName) { using (FileStream fs = new FileStream(FileName, FileMode.Create)) { XmlSerializer ser = new XmlSerializer(typeof(List<VisitHistoryPeople>)); ser.Serialize(fs, List); fs.Flush(); fs.Close(); } } public static List<VisitHistoryPeople> Load(string FileName) { if (!File.Exists(FileName)) throw new FileNotFoundException("The list file could not be found", FileName); List<VisitHistoryPeople> result; using (FileStream fs = new FileStream(FileName, FileMode.Open)) { XmlSerializer ser = new XmlSerializer(typeof(List<VisitHistoryPeople>)); result = (List<VisitHistoryPeople>)ser.Deserialize(fs); } return result; } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д