views:

179

answers:

1

I am serializing a Dictionary to XML. When I create a new dictionary I use the constructor to provide EqualityComparer without casing for instance

var tabs = new Dictionary<string,Tab>(StringComparer.OrdinalIgnoreCase);

I then serialize to XML and when I deserialize information about casing is lost - the deserialization is made to the Dictionary with GenericEqualityComparer, which apparently is case sensitive, because it doesn't find my keys if they are not cased correctly.

Any ideas how can I change it?

One way would be to create a new dictionary and copy the data from the deserialized over to the new one but this seems troublesome.

UPDATE:

The deserialization worked the whole time it is just that it deserializes the serialized Dictionary to one that does not use case insensitive keys.

A: 

Edit:

Per the comments, it appears this approach may be outdated in .NET 4.

End Edit

Dictionaries happen to require a little help to serialize and deserialize.

Here is a good example of an XML Serializable dictionary:

http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

You can make it case insensitive by changing the class declaration and adding a constructor, and tweaking a line.

public class SerializableDictionary<string, TValue>
    : Dictionary<string, TValue>, IXmlSerializable
{
    public SerializableDictionary()
        : base(StringComparer.InvariantCultureIgnoreCase)
    {
    }

    // ...
}

Change the line this.Add(key, value); to this[key] = value;.

At any rate, you may need to massage some of the details, but this should get you well on the road.

kbrimington
Working with .NET 4 it seems that the generic Dictionary is serializable, as it is working for me. I am not sure if implementing a solution from 2006 is advisable, given that the new Dictionary implementation or the DataContractSerializer's implementation supports Dictionary serialization/deserialization out-of-the-box.
mare
@mare: Hey, that's good news! Please consider updating the tag to .NET 4, and I'll place an edit on my post.
kbrimington