{"record":{"id":"609a07b0b8b2c0f2","repo":"dotnet/wpf","slug":"sr-invaliddeserialize","errorCode":null,"errorMessage":"SR.InvalidDeSerialize","messagePattern":"SR\\.InvalidDeSerialize","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializer.cs","lineNumber":58,"sourceCode":"        }\n        \n        #endregion Construction\n\n        #region OtherConversions\n        \n        /// <summary>\n        ///   Convert from Xaml read by a token reader into baml being written\n        ///   out by a record writer.  The context gives mapping information.\n        /// </summary>\n#if !PBTCOMPILER\n#endif        \n        internal virtual void ConvertXamlToBaml (\n            XamlReaderHelper          tokenReader,\n            ParserContext       context,\n            XamlNode            xamlNode,\n            BamlRecordWriter    bamlWriter)\n        {\n            throw new InvalidOperationException(SR.InvalidDeSerialize);\n        }\n\n#if !PBTCOMPILER\n\n        /// <summary>\n        ///   Convert from Xaml read by a token reader into a live\n        ///   object tree.  The context gives mapping information.\n        /// </summary>\n        internal virtual void ConvertXamlToObject (\n            XamlReaderHelper             tokenReader,\n            ReadWriteStreamManager streamManager,\n            ParserContext          context,\n            XamlNode               xamlNode,\n            BamlRecordReader       reader)\n        {\n            throw new InvalidOperationException(SR.InvalidDeSerialize);\n        }\n","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializer.cs#L40-L76","documentation":"XamlSerializer.ConvertXamlToBaml is a virtual method whose base implementation always throws InvalidOperationException(SR.InvalidDeSerialize). It is called (e.g. from WriteElementStart) when a serializer that does not support XAML-to-BAML conversion is asked to convert a node, meaning the concrete XamlSerializer subclass never overrode this method.","triggerScenarios":"Invoking a custom/derived XamlSerializer's ConvertXamlToBaml (via BamlRecordWriter / WriteElementStart) on a serializer type that only supports the live-object path and did not override ConvertXamlToBaml.","commonSituations":"Using XamlWriter.Save/BAML compilation pipelines with a serializer subclass lacking the conversion override; applying an optimized serializer (designed only for object<->binary) to the XAML->BAML path.","solutions":["Override ConvertXamlToBaml in your XamlSerializer subclass to perform the conversion (or delegate to the appropriate base).","Use a serializer class that supports the XAML-to-BAML direction (e.g. the appropriate ValueSerializer type for the value).","Check which serializer was registered for the type and ensure it matches the pipeline direction being executed."],"exampleFix":"// before\nclass MySerializer : XamlSerializer { }\n// after\nclass MySerializer : XamlSerializer {\n    internal override void ConvertXamlToBaml(XamlReaderHelper tokenReader, ParserContext context, XamlNode node, BamlRecordWriter writer) {\n        // implement conversion or call base helper\n    }\n}","handlingStrategy":"type-guard","validationCode":"var m = serializer.GetType().GetMethod(\"ConvertXamlToBaml\", BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);\nbool canConvert = m != null && m.DeclaringType != typeof(XamlSerializer);","typeGuard":"static bool SupportsXamlToBaml(XamlSerializer s) => s.GetType().GetMethod(\"ConvertXamlToBaml\", BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public)!.DeclaringType != typeof(XamlSerializer);","tryCatchPattern":"try { serializer.ConvertXamlToBaml(reader, ctx, node, writer); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"serialize\") || ex.Message.Contains(\"DeSerialize\")) { log.Error($\"{serializer.GetType().Name} does not support XAML->BAML\"); throw new NotSupportedException(ex.Message, ex); }","preventionTips":["Override all conversion directions when subclassing XamlSerializer.","Match the serializer to the pipeline direction (write vs read).","Unit-test custom serializers through both BAML write and read paths."],"tags":["wpf","xaml","baml","serialization"],"backgroundTag":"abstract-method-not-implemented","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"}