c# - 无法转换类型为 'System.Collections.Generic.List` 1 的对象

标签 c# .net xml list serialization

我不明白对另一种类型“System.Collections.Generic.List`1[CES.Model.SearchResult]”的引用以及如何解决此问题。

Unable to cast object of type 'System.Collections.Generic.List`1[CES.Model.SearchResult]' to type 'CES.Model.SearchResult'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[CES.Model.SearchResult]' to type 'CES.Model.SearchResult'.

Source Error: 


Line 1069:            XmlSerializer xs = new XmlSerializer(typeof(SearchResult));
Line 1070:            TextWriter textWriter = new StreamWriter(@"C:\temp\results.xml");
Line 1071:            xs.Serialize(textWriter, results);
Line 1072:            ViewState["eventList"] = textWriter.ToString();
Line 1073:            textWriter.Close();

这是 searchResult 类,它包含 SearchResultAttribute 类。

public class SearchResult
{
    private List<SearchResultAttribute> _attributes = null;
    private List<SearchResult> _linkedSearchResults = null;

    public string this[string key]
    {
        get
        {
            int resultIndex = _attributes.BinarySearch(new SearchResultAttribute(key, ""));

            if (resultIndex < 0)
                return "";
            else
                return _attributes[resultIndex].Value;
        }
        set
        {
            int resultIndex = _attributes.BinarySearch(new SearchResultAttribute(key, ""));

            if (resultIndex < 0)
                return;
            else
                _attributes[resultIndex].Value = value;
        }
    }

    public List<SearchResultAttribute> Attributes
    {
        get
        {
            return _attributes;
        }
        set
        {
            _attributes = value;
        }
    }
    public List<SearchResult> LinkedSearchResults
    {
        get
        {
            return _linkedSearchResults;
        }
        set
        {
            _linkedSearchResults = value;
        }
    }

    public SearchResult()
    {
        _attributes = new List<SearchResultAttribute>();
        _linkedSearchResults = new List<SearchResult>();
    }
}

public class SearchResultAttribute:IComparer<SearchResultAttribute>,IComparable<SearchResultAttribute>
{
    public string Key { get; set; }
    public string Value { get; set; }

    public SearchResultAttribute()
    {
        Key = System.String.Empty;
        Value = System.String.Empty;
    }

    public SearchResultAttribute(string key, string value)
    {
        Key = key;
        Value = value;
    }

    public int Compare(SearchResultAttribute x, SearchResultAttribute y)
    {
        return (x.Key.CompareTo(y.Key));
    }

    public int CompareTo(SearchResultAttribute other)
    {
        return this.Key.CompareTo(other.Key);
    }

}

感谢您的宝贵时间。

最佳答案

也许这应该有效:

XmlSerializer xs = new XmlSerializer(typeof(List<SearchResult>));

实际上,该消息让我认为xml包含搜索结果的集合,而不是单个搜索结果。

[编辑] DJ KRAZE 是对的,此代码假定“结果”变量属于 List<SearchResult> 类型.序列化程序必须匹配它将序列化的对象类型。

关于c# - 无法转换类型为 'System.Collections.Generic.List` 1 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11461238/

相关文章:

c# - 无法在 Visual Studio 中放置行号

c# - 为什么 Environment.NewLine = "\r\n"而字符串文字中的 "\n"功能相同?

c# - InvokeRequired 在新创建的 Dialog 上

java - 将数据保存在 XML 文件中

ruby - XML 到哈希转换 : Nori drops the attributes of the deepest XML elements

c# - 为什么继承嵌套接口(interface)会导致 C# 中的依赖循环?

c# - 如何用C#读取TD的值?

c# - 带有 Mvvm Light 处理鼠标和触摸的 WPF

c# - Foreach 更改未保留在 Linq 项目集合中

xml - XSLT 复制所有节点,并按分隔符分割