Collectable<T> serialization, Root Namespaces on T in .xml files.
- by Stacey
I have a Repository Class with the following method...
public T Single<T>(Predicate<T> expression)
{
using (var list = (Models.Collectable<T>)System.Xml.Serializer.Deserialize(typeof(Models.Collectable<T>), FileName))
{
return list.Find(expression);
}
}
Where Collectable is defined..
[Serializable]
public class Collectable<T> : List<T>, IDisposable
{
public Collectable() { }
public void Dispose() { }
}
And an Item that uses it is defined..
[Serializable]
[System.Xml.Serialization.XmlRoot("Titles")]
public partial class Titles : Collectable<Title>
{
}
The problem is when I call the method, it expects "Collectable" to be the XmlRoot, but the XmlRoot is "Titles" (all of object Title).
I have several classes that are collected in .xml files like this, but it seems pointless to rewrite the basic methods for loading each up when the generic accessors do it - but how can I enforce the proper root name for each file without hard coding methods for each one? The [System.Xml.Serialization.XmlRoot] seems to be ignored.