Сериализация и десериализация. Записывается только первая строка - C#
Формулировка задачи:
Всем добрый вечер. Подскажите если можете в чем может быть проблема.
У меня есть коллекция которую я сериализую в xml фалик. Вроде файлик создается, код его будет ниже. Но когда провожу десириализацию, проходит она без ошибок, но в коллекцию записывается первая строка, но столько раз сколько необходимо было по коллекции. Т.е. сериализировал я 21 элемент и потом 21 элемент записался, только одинаковый
Сериализация:
Десириализация:
Подскажите, в чем может быть проблема?
XmlSerializer xml = new XmlSerializer(typeof(List<Questions>));
FileStream fs = new FileStream("C:\\name\\proba.xml", FileMode.OpenOrCreate);
stud_class.xml.Serialize(fs, stud_class.Question);
fs.Close(); XmlSerializer xml = new XmlSerializer(typeof(List<Questions>));
FileStream fs = new FileStream("C:\\name\\proba.xml", FileMode.OpenOrCreate);
stud_class.Question = (List<Questions>)stud_class.xml.Deserialize(fs);
fs.Close();<?xml version="1.0"?>
<ArrayOfStudents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Students>
<Login>rostik</Login>
<Surname>Виталий</Surname>
<Firstname>Кулишов</Firstname>
<Group>КИ-10-4</Group>
<Count>7</Count>
</Students>
<Students>
<Login>rostik1</Login>
<Surname>Vitalii</Surname>
<Firstname>Kulishov</Firstname>
<Group>KI-10-4</Group>
<Count>0</Count>
</Students>
<Students>
<Login>asdfdsfa</Login>
<Surname />
<Firstname />
<Group />
<Count>0</Count>
</Students>
<Students>
<Login>rostik1</Login>
<Surname>Vitalii</Surname>
<Firstname>Kulishov</Firstname>
<Group>KI-10-4</Group>
<Count>0</Count>
</Students>
</ArrayOfStudents>
Решение задачи: «Сериализация и десериализация. Записывается только первая строка»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student(){FirstName="Alex",SecondName="Simpson",Group=10},
new Student(){FirstName="Will",SecondName="Smith",Group=25}
};
//Сериализуем
SerializeList(list);
Console.WriteLine("Operation completed!");
Console.WriteLine("Deserialized from file: ");
//Десериализуем
List<Student> ListFromFile = GetStudents();
//Выводим на консоль
foreach (Student s in ListFromFile)
{
Console.WriteLine("First name: {0}, Second name: {1}, Grop: {2}",
s.FirstName, s.SecondName, s.Group);
}
Console.ReadLine();
}
static void SerializeList(List<Student> list)
{
XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Student>));
using (Stream fs = new FileStream(@"C:\MyStudents.xml", FileMode.Create,
FileAccess.Write, FileShare.None))
{
xmlSerial.Serialize(fs, list);
}
}
static List<Student> GetStudents()
{
List<Student> tempList;
XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Student>));
using (Stream fs = new FileStream(@"C:\MyStudents.xml", FileMode.Open,
FileAccess.Read, FileShare.None))
{
tempList = (List<Student>)xmlSerial.Deserialize(fs);
}
return tempList;
}
}
[Serializable]
public class Student
{
public string FirstName { get; set; }
public string SecondName { get; set; }
public int Group { get; set; }
}
}