Night Walker
Night Walker

Reputation: 21280

serialization of IList<object[]> XmlSerializer with Generic Lists

I am getting some type IList<object[]> , what is the best way to serialize it to xml. And then read it back to IList<object[]>.

I just not see any easy way to do so.

Thanks for help.

Upvotes: 2

Views: 2262

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039468

The XmlSerializer chokes on interfaces. So you could convert it to an array or a concrete List<T> before serializing. Also you should absolutely specify known types because this object[] will simply not work. The serializer must know in advance all types that you will be dealing with. This way it will emit type information into the resulting XML:

var data = list.ToArray();
var knownTypes = new[] { typeof(Foo), typeof(Bar) };
var serializer = new XmlSerializer(data.GetType(), knownTypes);
serializer.Serialize(someStream, data);

Or if you don't wanna bother with all this and simply get some human readable persistence for your objects you could use Json:

var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(list);

And if you don't care about human readability, a binary serializer should do just fine.

Upvotes: 6

Related Questions