0

The documentation for the XmlSerializer.Serialize method states the following:

The XmlSerializer cannot serialize the following: arrays of ArrayList and arrays of List<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();
    }
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
  • Post the relevant parts of the classes involved. Right now it's unclear what you're talking about. – H H Jul 07 '12 at 16:59

1 Answers1

4

Read the documentation again - it says you can't serialize arrays of List<T> or ArrayList (i.e. List<T>[] and ArrayList[]).

Sure enough, these work:

Serialise(new ArrayList());
Serialise(new List<int>());

These do not:

Serialise(new ArrayList[]{});
Serialise(new List<int>[]{});

The latter throw this exception:

System.InvalidOperationException: Unable to generate a temporary class (result=1).

Adam
  • 15,537
  • 2
  • 42
  • 63
Jonathan Rupp
  • 15,522
  • 5
  • 45
  • 61