GenericDictionary Sample

Back to summary

Source

using System.Collections.Generic;

namespace XmlSerializersTest.Samples
{
    [Title("Generic dictionary")]
    public class GenericDictionary
    {
        public Dictionary<int, string> Things { get; set; }
    }
}

Standard Serializer

System.InvalidOperationException: There was an error reflecting type 'XmlSerializersTest.Samples.GenericDictionary'. ---> System.NotSupportedException: Cannot serialize member XmlSerializersTest.Samples.GenericDictionary.Things of type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.
   at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)
   at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)
   at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type)
   at XmlSerializersTest.Serializers.StandardXmlSerializer.Serialize(Object obj, XmlWriter writer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\Serializers\StandardXmlSerializer.cs:line 13
   at XmlSerializersTest.SampleProcessor.SerializeToString(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 162
   at XmlSerializersTest.SampleProcessor.RecordSerializationResults(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 115

Deserialization skipped

XAML Serializer

<GenericDictionary xmlns="clr-namespace:XmlSerializersTest.Samples;assembly=XmlSerializersTest" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <GenericDictionary.Things>
    <s:String x:Key="1">one</s:String>
    <s:String x:Key="2">two</s:String>
  </GenericDictionary.Things>
</GenericDictionary>

Deserialization failed:

System.Windows.Markup.XamlParseException: '1' attribute in 'http://schemas.microsoft.com/winfx/2006/xaml' namespace is valid only when used with an IDictionary property. Cannot add element to 'Things'; the property value is null.  Error at Line 3 Position 15.  Error at Line 3 Position 15. ---> System.Windows.Markup.XamlParseException: Cannot add element to 'Things'; the property value is null.  Error at Line 3 Position 15.
   at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
   at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
   at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException)
   at System.Windows.Markup.BamlRecordReader.ThrowException(String id, String parameter)
   at System.Windows.Markup.BamlRecordReader.InitPropertyCollection(BamlCollectionHolder holder, ReaderContextStackData context)
   at System.Windows.Markup.BamlRecordReader.GetCollectionHolderFromContext(ReaderContextStackData context, Boolean toInsert)
   at System.Windows.Markup.BamlRecordReader.GetDictionaryFromContext(ReaderContextStackData context, Boolean toInsert)
   at System.Windows.Markup.BamlRecordReader.SetKeyOnContext(Object key, String attributeName, ReaderContextStackData context, ReaderContextStackData parentContext)
   --- End of inner exception stack trace ---
   at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
   at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
   at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException)
   at System.Windows.Markup.BamlRecordReader.SetKeyOnContext(Object key, String attributeName, ReaderContextStackData context, ReaderContextStackData parentContext)
   at System.Windows.Markup.BamlRecordReader.ReadDefAttributeRecord(BamlDefAttributeRecord bamlDefAttributeRecord)
   at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
   at System.Windows.Markup.BamlRecordReader.Read(BamlRecord bamlRecord, Int32 lineNumber, Int32 linePosition)
   at System.Windows.Markup.XamlTreeBuilder.BamlRecordWriterSyncUpdate(BamlRecord bamlRecord, Int32 lineNumber, Int32 linePosition)
   at System.Windows.Markup.XamlTreeBuilderBamlRecordWriter.WriteBamlRecord(BamlRecord bamlRecord, Int32 lineNumber, Int32 linePosition)
   at System.Windows.Markup.BamlRecordWriter.WriteAndReleaseRecord(BamlRecord bamlRecord, XamlNode xamlNode)
   at System.Windows.Markup.BamlRecordWriter.WriteDefAttribute(XamlDefAttributeNode xamlDefNode)
   at System.Windows.Markup.XamlParser.WriteDefAttribute(XamlDefAttributeNode xamlDefAttributeNode)
   at System.Windows.Markup.XamlParser.WriteDefAttributeCore(XamlDefAttributeNode xamlDefAttributeNode)
   at System.Windows.Markup.XamlParser.ProcessXamlNode(XamlNode xamlNode, Boolean& cleanup, Boolean& done)
   at System.Windows.Markup.XamlParser.ReadXaml(Boolean singleRecordMode)
   at System.Windows.Markup.TreeBuilderXamlTranslator._Parse()
   at System.Windows.Markup.XamlParser.Parse()
   at System.Windows.Markup.XamlTreeBuilder.ParseFragment()
   at System.Windows.Markup.TreeBuilder.Parse()
   at System.Windows.Markup.XamlReader.XmlTreeBuildDefault(ParserContext pc, XmlReader reader, Boolean wrapWithMarkupCompatReader, XamlParseMode parseMode, Boolean etwTracingEnabled)
   at System.Windows.Markup.XamlReader.Load(XmlReader reader)
   at XmlSerializersTest.Serializers.XamlSerializer.Deserialize(XmlReader reader, Type typeToDeserialize) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\Serializers\XamlSerializer.cs:line 18
   at XmlSerializersTest.SampleProcessor.DeserializeFromString(String xml, IXmlSerializer serializer, Type typeToDeserialize) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 198
   at XmlSerializersTest.SampleProcessor.RecordDeserializationResults(String xml, IXmlSerializer serializer, Type typeToDeserialize) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 181

WCF Serializer

<GenericDictionary xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">
  <Things xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfintstring>
      <d2p1:Key>1</d2p1:Key>
      <d2p1:Value>one</d2p1:Value>
    </d2p1:KeyValueOfintstring>
    <d2p1:KeyValueOfintstring>
      <d2p1:Key>2</d2p1:Key>
      <d2p1:Value>two</d2p1:Value>
    </d2p1:KeyValueOfintstring>
  </Things>
</GenericDictionary>

WCF-ref Serializer

<GenericDictionary xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">
  <Things xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Id="2" z:Size="2">
    <d2p1:KeyValueOfintstring>
      <d2p1:Key>1</d2p1:Key>
      <d2p1:Value z:Id="3">one</d2p1:Value>
    </d2p1:KeyValueOfintstring>
    <d2p1:KeyValueOfintstring>
      <d2p1:Key>2</d2p1:Key>
      <d2p1:Value z:Id="4">two</d2p1:Value>
    </d2p1:KeyValueOfintstring>
  </Things>
</GenericDictionary>