Witam.
Mam problem z serializacją Listy obiektów np. książek.
Kod :
book.cs
public class Book
{
public Book(int number, string title)
{
this.title = title;
this.number = number;
}
public int number { get; set; }
public string title { get; set; }
}
DataContext
[Serializable]
[XmlRoot("DataContext")]
public class DataContext
{
public List<Book> listOfBooks { get; set; }
public DataContext()
{
}
Program.cs
public class Program
{
static void Main(string[] args)
{
DataContext context = new DataContext();
List<Book> listOfBooks = new List<Book>();
listOfBooks.Add(new Book(1, "hehehe"));
listOfBooks.Add(new Book(100, "sdjksfdgjh"));
context.listOfBooks = listOfBooks;
FileStream fs = new FileStream("hehe.xml", FileMode.OpenOrCreate);
System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(DataContext));
s.Serialize(fs, context);
}
}
Błąd jaki dostaje to : DataContext nie może być serializowany, ponieważ nie ma domyślnego konstruktora bez parametrów.
Proszę o jakieś podpowiedzi.