0

I'm trying to send a XML based on this entity :

[XmlElement]
public decimal Price { get; set; }
[XmlElement]
public Dictionary<string, string> Data{get; set;}

Before the addition of the latest parameter, there was no problem but the addition of the Dictionary causes trouble.

The Exception is :

Error : There was an error reflecting type 'Project.Entities.Offers'.

Here is the Serializer and the line that cause the problem

The XmlSerializer is doing an Exception

public string Serialize<T>()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
...
}

Is it a solution to that problem? a workaround?

Thanks to help me

clement
  • 4,204
  • 10
  • 65
  • 133

1 Answers1

0

Dictionary<TKey,TValue> is not marked serializeable. The reasoning for this is you can not enforce the uniqueness constraints while the dictionary is in XML form, there is nothing from stopping me from doing something like

<Dictionary>
    <DictionaryElement Key="1" Value="Foo"/>
    <DictionaryElement Key="1" Value="Bar"/>
</Dictionary>

However people usually write wrappers to make a dictionary serializeable, but you will need to decide if it will either silently skip duplicate keys or throw an exception on duplicate keys.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431