{"record":{"id":"4b662bf0ea661734","repo":"dotnet/wpf","slug":"sr-malformedbracketcharacters","errorCode":null,"errorMessage":"SR.MalformedBracketCharacters","messagePattern":"SR\\.MalformedBracketCharacters","errorType":"exception","errorClass":"XamlParseException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/MeScanner.cs","lineNumber":497,"sourceCode":"                            bracketCharacterStack.Push(ch);\n                            _context.CurrentBracketModeParseParameters.IsBracketEscapeMode = true;\n                        }\n\n                        sb.Append(ch);\n                        break;\n                    }\n\n                    if (done)\n                    {\n                        if (braceCount > 0)\n                        {\n                            throw new XamlParseException(this, SR.UnexpectedTokenAfterME);\n                        }\n                        else\n                        {\n                            if (_context.CurrentBracketModeParseParameters?.BracketCharacterStack.Count > 0)\n                            {\n                                throw new XamlParseException(this, SR.Format(SR.MalformedBracketCharacters, ch.ToString()));\n                            }\n                        }\n\n                        PushBack();\n                        break;  // we are done.\n                    }\n                }\n\n                atStart = false;\n                Advance();\n            }\n\n            if (quoteChar != NullChar)\n            {\n                throw new XamlParseException(this, SR.UnclosedQuote);\n            }\n\n            string result = sb.ToString();","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/MeScanner.cs#L479-L515","documentation":"At the end of a markup-extension string, MeScanner checks the bracket stack kept in CurrentBracketModeParseParameters. If closing bracket characters remain unmatched (BracketCharacterStack.Count > 0) when the extension terminates, it throws XamlParseException formatted with SR.MalformedBracketCharacters and the leftover character. It guarantees extension boundaries do not silently absorb unbalanced brackets.","triggerScenarios":"XAML where an extension value opens a bracket that never closes before the extension ends, e.g. `Text=\"{StaticResource [Key}\"` or `Text=\"{Binding Path=(Foo.Bar}\"`.","commonSituations":"Values containing literal '[' or '(' pasted into markup extensions unescaped; code that programmatically assembles extension strings dropping a closer; WPF designer round-trips of hand-edited markup.","solutions":["Add the missing closing bracket reported in the message inside the extension value.","Escape literal brackets/braces by prefixing the value with {} when the content is not a real extension.","Store literal bracket-heavy text in a resource file instead of an inline markup extension.","Validate with a minimal XamlReader.Load snippet to locate the attribute and character."],"exampleFix":"<!-- before -->\n<TextBlock Text=\"{StaticResource [Key}\" />\n\n<!-- after -->\n<TextBlock Text=\"{StaticResource Key}\" />","handlingStrategy":"validation","validationCode":"static bool NoUnmatchedBrackets(string value) { var stack = new Stack<char>(); foreach (var c in value) { if (c=='('||c=='[') stack.Push(c); else if (c==')') { if (stack.Pop()!='(') return false; } else if (c==']') { if (stack.Pop()!='[') return false; } } return stack.Count==0; }","typeGuard":null,"tryCatchPattern":"try { return XamlReader.Parse(xaml); } catch (XamlParseException ex) { return FallbackValue; }","preventionTips":["Escape literal brackets with {} prefix when content is not an extension","Match bracket kinds exactly '(' vs '['","Keep bracket-heavy literals in resx files"],"tags":["xaml","markup-extension","bracket-mismatch","parser"],"backgroundTag":"invalid-argument-format","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"}