1

I am getting the following error when I try to serialize List<SLItem>:

There was an error reflecting type 'System.Collections.Generic.List`1

[Serializable]
public class SLItem
{
    public int ID { get; set; }
    public string ActorName { get; set; }  
    public Dictionary<string, List<string>> VerLookup { get; set; }
    public bool IsEnabled { get; set; }
}

Am I missing something here.

3 Answers 3

2

Instead of the Dictionary use a list of:

[Serializable]
public struct KeyValuePair<K,V>
{
  public K Key {get;set;}
  public V Value {get;set;}
}

So your Dictionary becomes:

List<KeyValuePair<string, List<string>>>

XmlSerializer cannot serialize classes that have read-only properties. Dictionaries contain KeyValuePair<>'s which are implemented with read-only key and value properties.

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

Comments

0

I don't think you can serialize the Dictionary. This post may help: There was an error reflecting type - XML Serialization issue

Comments

0

You can make a child class which implements IXmlSerializable. Some time ago I've created it and serialization to XML works. Here is the code!

using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;


[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members

    public XmlSchema GetSchema()
    {
        return null;
    }


    public void ReadXml(XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();
        if (wasEmpty)
            return;

        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            reader.ReadStartElement("key");
            TKey key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("value");
            TValue value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();
            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }

    #endregion
}

Comments

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.