I have seen a lot of issues using the XmlSerializer on SO, and yes most of the issues seem to be around complex objects.
Mine issue is the same in essence, but with a twist.
I have used the Entityframework POCO generator to create my database objects.
Now, I am trying to compare complex objects using XmlSerializer.
So in my save, I am doing the following:
viewModelObj = returned object model from MVC page.
db.originalData.ToList() = original data object
var a = SerializeObject(viewModelObj);
var b = SerializeObject(db.originalData.ToList());
with the following definition for my SerializeObject function.
public static string SerializeObject(this List<myObject> toSerialize)
{
var xmlSerializer = new XmlSerializer(toSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
}
Because myObject repersents my POCO classes, Lists are defined as ICollection, which in turn causes the XmlSerializer to fail with the following message:
Cannot serialize member toSerialize of type System.Collections.Generic.ICollection`1[[myObject]] because it is an interface.
Which is the correct response.
Every time i run the EF POCO Generator, it changes everything back to ICollections, so my question is how can i use XmlSerializer using the POCO classes?
Update
Changed code from
public static string SerializeObject(this List<myObject> toSerialize)
to
public static string SerializeObject<T>(this List<T> toSerialize)
This is when I get the error message:
Cannot serialize member [object] of type System.Collection.Generic.ICollection'1[[object]] because it is an interface.
Just given up. re coded around this issue by adding a status field and checking to see if it has changed.