The documentation for the XmlSerializer.Serialize method states the following:
The
XmlSerializer
cannot serialize the following: arrays ofArrayList
and arrays ofList<T>
.
However if I try with the following code it works (I am use List<int>
and ArrayList
). So is this a documentation defect, a new feature in .NET 4.5 that hasn't made it's way to documentation?
I had suspected that it could be an abbreviated message about how you cannot serialise a List<T>
unless you have all the types in object graph, but that doesn't make sense for ArrayList which is just object
.
private static string Serialise<T>(T o)
{
var serializer = new XmlSerializer(typeof(T));
var memoryStream = new MemoryStream();
serializer.Serialize(memoryStream, o);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
return reader.ReadToEnd();
}
}