{"record":{"id":"5d7c49267340df51","repo":"dotnet/wpf","slug":"sr-unserializablekeyvalue","errorCode":null,"errorMessage":"SR.UnserializableKeyValue","messagePattern":"SR\\.UnserializableKeyValue","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs","lineNumber":501,"sourceCode":"            MarkupProperty contentProperty = null;\n\n            bool first = true;\n            int argumentCount = 0;\n            List<int> argumentCompositeIndexes = null;\n            bool noOtherPropertiesAllowed = false;\n            List<MarkupProperty> composites = null;\n            Dictionary<string, string> writtenAttributes = new Dictionary<string, string>();\n            PartiallyOrderedList<string, MarkupProperty> deferredProperties = null;\n            Formatting previousFormatting = (_xmlTextWriter != null) ? _xmlTextWriter.Formatting : Formatting.None;\n\n            foreach (MarkupProperty property in item.GetProperties(false /*mapToConstructorArgs*/))\n            {\n                if (property.IsConstructorArgument)\n                {\n                    // When the reader supports <x:Argument1>...</x:Argument1> format, do the following:\n                    //   _writer.WriteStartElement(string.Format(CultureInfo.InvariantCulture, \"Argument{0}\", argumentCompositeIndexes[argumentCompositeIndex++]), NamespaceCache.XamlNamespace);\n                    // The writer generates an exception for now:\n                    throw new InvalidOperationException(SR.UnserializableKeyValue);\n                }\n                Debug.Assert(!noOtherPropertiesAllowed || property.IsKey,\n                    \"Problem with MarkupObject implemenation: Items returning a ValueAsString can have no other properties\");\n\n                if (IsContentProperty(property, cpa, ref contentProperty))\n                {\n                    first = false;\n                    continue;\n                }\n                if (IsDeferredProperty(property, writtenAttributes, ref deferredProperties))\n                {\n                    continue;\n                }\n\n                if (!property.IsComposite)\n                {\n                    if (property.IsAttached || property.PropertyDescriptor == null)\n                    {","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs#L483-L519","documentation":"MarkupWriter.WriteItem throws this plain InvalidOperationException (SR.UnserializableKeyValue) when a MarkupObject yields a property flagged IsConstructorArgument. The XAML writer cannot emit constructor arguments (the reader-side <x:ArgumentN> format was never implemented), so any object requiring constructor-argument properties cannot be serialized. The surrounding comment marks it as an intentional stop-gap exception.","triggerScenarios":"Serializing (XamlWriter.Save / MarkupWriter.WriteItem) a type whose markup provider exposes IsConstructorArgument properties - e.g. types with required constructor parameters mapped by WPF markup metadata as constructor arguments.","commonSituations":"Saving objects of types whose value-type marshaling is defined through constructor arguments (e.g. some structs via their markup implementation); this indicates the type cannot be round-tripped by this writer.","solutions":["Avoid serializing that type with MarkupWriter/XamlWriter; serialize its properties individually or via a convertible surrogate type.","Add a TypeConverter with ConvertToString/ConvertFrom support so the value is emitted as a string instead of constructor-argument properties.","Provide a designer markup override (AttributeTable / ValueSerializer) that eliminates the constructor-argument property."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// any property with IsConstructorArgument => unsupported\nbool HasConstructorArgumentProps(object o) =>\n    MarkupWriter.GetMarkupObjectFor(o).Properties.Any(p => p.IsConstructorArgument);","typeGuard":null,"tryCatchPattern":"try { XamlWriter.Save(obj, stream); }\ncatch (InvalidOperationException ex)\n{\n    // use DataContractSerializer or TypeConverter-based output instead\n}","preventionTips":["Prefer serializing types with default constructors and settable public properties.","Provide TypeConverters for value types that would otherwise need constructor arguments.","Avoid XamlWriter for types known to use constructor-argument markup."],"tags":["wpf","xaml","serialization"],"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-22T01:17:13.364Z"}