How can I use a list inside a XML Serializer class?

I am trying to work with XML Serializer now since it seems more flexible than XMLTextReaders are. I’m trying to use it to create a tree that contains multiple elements of the same type that would ideally look something like this.


I have tried using a list to declare the class that serializes the xml file but I can’t see to get it to work, I’d rather not have to use an array if I can avoid it because I it doesn’t have that nice dynamic quality like a list. Currently my code looks like this.

public class library{ 
public List<Books> book; 
public class Books{ 
public string Book;  

If anyone can help me I’d greatly appreciate it. I’m having a pretty hard time wrapping my head around the XML Serializer. Thanks.

edit: Sorry for all the revisions I had to work to get the formatting to display correctly.

Your XML output file would probably look more like this:


You would need to use it like this (untested):

Library library = new Library();
library.books.Add(new Book());

XmlSerializer serializer = new XmlSerializer(typeof(Library));
TextWriter textWriter = new StreamWriter(Application.dataPath + "Resources");
serializer.Serialize(textWriter, library);

Here are the class definitions:

public class Library {
    public List<Book> books;

public class Book {
    public string name;