Próbuję zeobić serializację w C#.
Mam taki kod:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person() { }
public Person(string imie, string nazwisko)
{
this.FirstName = imie;
this.LastName = nazwisko;
}
using System.IO;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace Seria
{
class Program
{
static void Main(string[] args)
{
//Person person = new Person();
List<Person> person = new List<Person>();
person.Add(new Person("Anna", "Karenina"));
person.Add(new Person("Adam", "Małysz"));
person.Add(new Person("Helga", "Niemra"));
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (Stream s = File.Create("people.xml"))
{
serializer.Serialize(s, person);
//serializer.Serialize(s, person3);
s.Close();
}
Console.ReadKey();
}
}
}
Ale gdy próbuję odpalić program wyskakuje mi błąd:
Wyjątek nieobsłużony: System.InvalidOperationException: Wystąpił błąd podczas generowania dokumentu XML. ---> System.InvalidCastException: Nie można rzutować obiektu typu 'System.Collections.Generic.List`1[Seria.Person]' na typ 'Seria.Person'.
Nie wiem jak mam to obejść. Możecie mi podpowiedzieć?