{"record":{"id":"32cdd3d6cba1f7a5","repo":"dotnet/wpf","slug":"sr-xamlmarkupextensionwriterinputinvalid","errorCode":null,"errorMessage":"SR.XamlMarkupExtensionWriterInputInvalid","messagePattern":"SR\\.XamlMarkupExtensionWriterInputInvalid","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlMarkupExtensionWriter.cs","lineNumber":337,"sourceCode":"                writer.nodes.Push(new Node { NodeType = XamlNodeType.StartObject, XamlType = type });\n                writer.currentState = InObjectBeforeMember.State;\n            }\n        }\n\n        private abstract class InObject : WriterState\n        {\n            protected InObject() { }\n\n            public abstract string Delimiter\n            {\n                get;\n            }\n\n            public override void WriteEndObject(XamlMarkupExtensionWriter writer)\n            {\n                if (writer.nodes.Count == 0)\n                {\n                    throw new InvalidOperationException(SR.XamlMarkupExtensionWriterInputInvalid);\n                }\n\n                Node node = writer.nodes.Pop();\n\n                if (node.NodeType != XamlNodeType.StartObject)\n                {\n                    throw new InvalidOperationException(SR.XamlMarkupExtensionWriterInputInvalid);\n                }\n\n                writer.sb.Append('}');\n\n                if (writer.nodes.Count == 0)\n                {\n                    writer.currentState = Start.State;\n                }\n                else\n                {\n                    Node member = writer.nodes.Peek();","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlMarkupExtensionWriter.cs#L319-L355","documentation":"WriteEndObject pops a node from the internal node stack to match the end object against its start object. If the stack is empty there is no matching StartObject, meaning the writer received an EndObject without a corresponding StartObject — an invalid XAML node stream. The library throws InvalidOperationException with XamlMarkupExtensionWriterInputInvalid.","triggerScenarios":"Calling XamlMarkupExtensionWriter.WriteEndObject() (or forwarding an EndObject node) before any WriteStartElement/WriteStartObject for the object has been issued, or calling it more times than StartObject was called.","commonSituations":"Hand-written node-stream emission that is off by one; a node-stream filter that drops StartObject nodes but forwards EndObject; replaying a truncated XAML node stream.","solutions":["Ensure every WriteEndObject is paired with exactly one prior WriteStartObject for the same object","Validate/normalize the XAML node stream before forwarding it (e.g. with a custom XamlWriter that checks balance)","Wrap the writing pass in try/catch for InvalidOperationException and log the offending node sequence"],"exampleFix":"// before\nif (nodeType == XamlNodeType.EndObject) writer.WriteEndObject(); // no state check\n// after\nif (nodeType == XamlNodeType.EndObject && startObjectDepth > 0) { startObjectDepth--; writer.WriteEndObject(); }","handlingStrategy":"validation","validationCode":"if (startObjectDepth <= 0) throw new InvalidOperationException(\"WriteEndObject called without an open StartObject\");","typeGuard":"bool CanEndObject => startObjectDepth > 0;","tryCatchPattern":"try { writer.WriteEndObject(); }\ncatch (InvalidOperationException ex) when (ex.Message == SR.XamlMarkupExtensionWriterInputInvalid || ex.Message.Contains(\"markup extension\")) { /* fix stream pairing */ }","preventionTips":["Maintain a depth counter for StartObject/EndObject in forwarding code","Never forward EndObject nodes from a filtered stream unless StartObject was forwarded","Replay streams through XamlNodeList to normalize before writing"],"tags":["xaml","invalid-operation-exception","node-stream"],"backgroundTag":"invalid-state-transition","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"}