Collectable<T> serialization, Root Namespaces on T in .xml files.

Posted by Stacey on Stack Overflow See other posts from Stack Overflow or by Stacey
Published on 2010-03-14T04:19:42Z Indexed on 2010/03/14 4:25 UTC
Read the original article Hit count: 223

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about xml-serialization