[Mono-list] XmlSerializer with Dictionary

Colin JN Breame colin at breame.net
Wed Jan 18 16:32:02 EST 2006


Hello,

When I try to xml serialise a generic dictionary (e.g. 
Dictionary<string,string>), I get the exception:

System.InvalidOperationException: You must implement a default accessor on 
System.Collections.Generic.Dictionary`2[snip] because it inherits from 
ICollection

This looks like it could be a bug but then again I'm not even sure it is 
possible to serialise a generic dictionary.  Any pointers would be 
appreciated.

Cheers,
Colin


test program:

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

public class main_t {
  public static void Main() {
    Dictionary<string, string> test = new Dictionary<string, string>();
    StreamWriter writer = new StreamWriter("output");

    XmlSerializer serialiser = new XmlSerializer(typeof(Dictionary<string,
                                                        string>));
    serialiser.Serialize(writer, test);
  }
}


exception:

Unhandled Exception: System.InvalidOperationException: You must implement a 
default accessor on System.Collections.Generic.Dictionary`2[[System.String, 
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],
[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089]] because it inherits from ICollection
in <0x001f7> System.Xml.Serialization.TypeData:get_ListItemType ()
in <0x0001f> System.Xml.Serialization.TypeData:get_ListItemTypeData ()
in <0x00145> System.Xml.Serialization.TypeData:.ctor (System.Type type, 
System.String elementName, Boolean isPrimitive, 
System.Xml.Serialization.TypeData mappedType, 
System.Xml.Schema.XmlSchemaPatternFacet facet)
in <0x00019> System.Xml.Serialization.TypeData:.ctor (System.Type type, 
System.String elementName, Boolean isPrimitive)
in <0x0032b> System.Xml.Serialization.TypeTranslator:GetTypeData (System.Type 
type, System.String xmlDataType)
in <0x0000c> System.Xml.Serialization.TypeTranslator:GetTypeData (System.Type 
type)
in <0x0005a> System.Xml.Serialization.XmlReflectionImporter:ImportTypeMapping 
(System.Type type, System.Xml.Serialization.XmlRootAttribute root, 
System.String defaultNamespace)
in <0x0009d> System.Xml.Serialization.XmlSerializer:.ctor (System.Type type, 
System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] 
extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String 
defaultNamespace)
in <0x00015> System.Xml.Serialization.XmlSerializer:.ctor (System.Type type)







More information about the Mono-list mailing list