7

I do not understand this reference to another type 'System.Collections.Generic.List`1[CES.Model.SearchResult]' and how to resolve this issue.

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();

Here is the searchResult class and it's contained SearchResultAttribute class.

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);
    }

}

Thank you for your time.

1 Answer 1

9

Maybe this should work :

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

In fact, the message let me think that the xml contains a collection of searchrestult, not a single search result.

[Edit] DJ KRAZE is right, this code assume that the "results" variable is of kind List<SearchResult>. The serializer has to match the type of object that it will serialize.

Sign up to request clarification or add additional context in comments.

2 Comments

Steve you may want to explain why you are using List<SearchResult> opposed to him trying to cast as SearchResult. this may help him to realize that casting should be of the same type in his case.. where he has private variable defined as List<> + 1 to you as well
Thanks Steve B. Another case of me not reading the error message carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.