The reason you are getting this error is because you are using an ArrayList
and the XmlSerializer doesn't know about your Person
class. One possibility is to indicate to the serializer as a known type when instantiating the serializer:
var serializer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
but a better way is to use a generic List<T>
instead of ArrayList. So let's suppose that you have the following model:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Now you could have a list of people:
List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "John", LastName = "Smith" });
people.Add(new Person { FirstName = "John 2", LastName = "Smith 2" });
that you could serialize:
using (var writer = XmlWriter.Create("fs.xml"))
{
var serializer = new XmlSerializer(typeof(List<Person>));
serializer.Serialize(writer, people);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…