{"record":{"id":"81193dce69340df8","repo":"dotnet/wpf","slug":"sr-format-sr-unserializablekeyvalue-property-value-gettype","errorCode":null,"errorMessage":"SR.Format(SR.UnserializableKeyValue, property.Value.GetType().FullName)","messagePattern":"SR\\.Format\\(SR\\.UnserializableKeyValue, property\\.Value\\.GetType\\(\\)\\.FullName\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs","lineNumber":693,"sourceCode":"                    bool propertyTagWritten = false;\n                    bool explicitTagWritten = false;\n\n                    foreach (MarkupObject subItem in property.Items)\n                    {\n                        if (!propertyTagWritten)\n                        {\n                            propertyTagWritten = true;\n                            // uri is made addressable above so it is not necessary here\n                            if (property.IsAttached || property.PropertyDescriptor == null)\n                            {\n                                Debug.Assert(!property.IsValueAsString, \"Problem with MarkupObject implementation: String values cannnot be composite\");\n\n                                if (property.IsKey)\n                                {\n                                    // When the reader supports <x:Key> ... </x:Key> format do the following:\n                                    //   _writer.WriteStartElement(\"Key\", NamespaceCache.XamlNamespace);\n                                    // The writer generates an exception for now:\n                                    throw new InvalidOperationException(SR.Format(SR.UnserializableKeyValue, property.Value.GetType().FullName));\n                                }\n                                else\n                                {\n                                    string dpUri = scope.MakeAddressable(property.DependencyProperty.OwnerType);\n                                    WritePropertyStart(scope.GetPrefixOf(dpUri),\n                                        $\"{property.DependencyProperty.OwnerType.Name}.{property.DependencyProperty.Name}\", dpUri);\n                                }\n                            }\n                            else\n                            {\n                                WritePropertyStart(prefix, $\"{item.ObjectType.Name}.{property.PropertyDescriptor.Name}\", uri);\n\n                                writtenAttributes[property.Name] = property.Name;\n                            }\n\n                            explicitTagWritten = NeedToWriteExplicitTag(property, subItem);\n\n                            if (explicitTagWritten)","sourceCodeStart":675,"sourceCodeEnd":711,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs#L675-L711","documentation":"MarkupWriter.WriteItem throws this InvalidOperationException (SR.UnserializableKeyValue with the property value's full type name) when a MarkupObject for a keyed item (e.g. an entry in a dictionary being serialized) exposes its key as an IsKey property. XAML serialization of keys is not implemented by this writer (the <x:Key> format was never added), so any keyed item is unserializable and the exception reports the key value's type.","triggerScenarios":"Calling XamlWriter.Save / MarkupWriter.WriteItem on a dictionary or keyed collection whose MarkupObject items expose IsKey properties - e.g. serializing a ResourceDictionary-like structure with entries the writer cannot emit.","commonSituations":"Serializing dictionaries of custom key types; saving resource dictionaries containing non-serializable entries; automating XAML round-trip of collections.","solutions":["Avoid serializing keyed dictionaries with XamlWriter.Save; write a custom serializer that emits entries explicitly.","Flatten the dictionary into a list of key/value wrapper objects (non-keyed) before saving.","Register a TypeConverter/ValueSerializer for the key type so items are expressed without an IsKey property."],"exampleFix":"// before\nvar dict = new Dictionary<string, MyData>();\nXamlWriter.Save(dict); // InvalidOperationException: UnserializableKeyValue\n// after\nvar list = dict.Select(kv => new Entry { Key = kv.Key, Value = kv.Value }).ToList();\nXamlWriter.Save(list); // public non-generic wrapper entries serialize fine","handlingStrategy":"try-catch","validationCode":"bool HasKeyedItems(object o) =>\n    MarkupWriter.GetMarkupObjectFor(o).Properties.Any(p => p.IsKey);","typeGuard":null,"tryCatchPattern":"try { XamlWriter.Save(obj, stream); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"key\"))\n{\n    // fall back to serializing entries as key/value wrapper objects\n}","preventionTips":["Don't use XamlWriter.Save on dictionaries or keyed collections.","Convert dictionaries to explicit entry wrapper lists before saving.","Test round-trips of any collection types you intend to persist as XAML."],"tags":["wpf","xaml","serialization","dictionary"],"backgroundTag":"operation-not-supported","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}