简体   繁体   English

序列化从通用集合派生的类,但反序列化通用集合

[英]Serializing Class Derived from Generic Collection yet Deserializing the Generic Collection

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.. 在何处定义了Collectable。

[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). 问题是当我调用该方法时,它期望“ Collectable”是XmlRoot,但是XmlRoot是“ Titles”(所有对象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? 我在.xml文件中收集了几个这样的类,但是当通用访问器这样做时,重写用于加载每个文件的基本方法似乎毫无意义-但是如何在没有硬编码方法的情况下为每个文件强制使用正确的根名每一个人? The [System.Xml.Serialization.XmlRoot] seems to be ignored. [System.Xml.Serialization.XmlRoot]似乎被忽略了。

When called like this... 当这样叫...

var titles = Repository.List<Models.Title>(); 

I get the exception 我例外

<Titlesxmlns=''> was not expected. 

The XML is formatted such as. XML的格式如。 .. ..

<?xml version="1.0" encoding="utf-16"?>
<Titles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Title>
        <Id>442daf7d-193c-4da8-be0b-417cec9dc1c5</Id>
    </Title>
</Titles>

Here is the deserialization code. 这是反序列化代码。

  public static T Deserialize<T>(String xmlString)
    {
        System.Xml.Serialization.XmlSerializer XmlFormatSerializer
            = new System.Xml.Serialization.XmlSerializer(typeof(T));

        StreamReader XmlStringReader = new StreamReader(xmlString);

        //XmlTextReader XmlFormatReader = new XmlTextReader(XmlStringReader);

        try
        {
            return (T)XmlFormatSerializer.Deserialize(XmlStringReader);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            XmlStringReader.Close();
        }
    }

It makes no sense for the root of a document to be a collection. 这是没有意义的文档的根一个集合。 It may contain a collection, but it cannot be one. 它可能包含一个集合,但不能一个。


Your problem appears to be simpler. 您的问题似乎更简单。 You cannot create a serializer using an open generic type ( Models.Collectable<T> ). 您不能使用开放的通用类型( Models.Collectable<T> )创建序列化程序。 Try using typeof(Titles) instead. 尝试改用typeof(Titles)


I'm making some assumptions about your repository classes, but would something like this work? 我正在对您的存储库类进行一些假设,但是这样的工作可行吗?

public class Repository<T>
{
    protected static TextReader FileName { get; set; }

    public static TCollection List<TCollection>()
    {
        var ser = new XmlSerializer(typeof (TCollection));
        return (TCollection) ser.Deserialize(FileName);
    }

    public static TItem Single<TItem, TCollection>(Predicate<TItem> expression) 
        where TCollection : IDisposable, IEnumerable<TItem>
    {
        using (var list = List<TCollection>())
        {
            return list.Single(item => expression(item));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM