C# Newb - XMLSerializing a List

Hi, I'm pretty new to C# and am trying to write a class with data in it that i can save and load from an XML file, are there any built in Generic Containers/Collection, something like List or Stack that XML can serialize, my code is below, but whatever container i use i get an exception complaining about how it can't serialize the collection i am using (List) in this case.

How do i have to go about serializing a collection of objects Is it something that can be done with a similar amoutn of code as below or am i dreaming

namespace Test
{
public class ClassA
{
public List<ClassB> Events;

public ClassA()
{
Events = new List<ClassB>();


Events.Add(new ClassB());
}

#region Load/Save Code

public void Save(string filename)
{
Stream stream = File.Create(filename);

XmlSerializer serializer = new XmlSerializer(typeof(ClassA));

serializer.Serialize(stream, this);
stream.Close();

return;
}

public static ClassA Load(string filename)
{
Stream stream = File.OpenRead(filename);

XmlSerializer serializer = new XmlSerializer(typeof(ClassA));

ClassA result = (ClassA) serializer.Deserialize(stream);

stream.Close();

return (result);
}

#endregion
}

public class ClassB
{
public int testVar;

public ClassB()
{
testVar = 1;

return;
}
}
}


Answer this question

C# Newb - XMLSerializing a List