dotnet/wpf · error · XamlParseException
brokenRule (SR.UnexpectedToken-based parser rule error)
Error message
brokenRule (SR.UnexpectedToken-based parser rule error)
What it means
MePullParser.Parse processes a markup extension string ('{...}'). If the tokenizer never found a valid markup-extension construct (f.found == false), the accumulated broken-rule description (_brokenRule) is thrown as a XamlParseException, typically carrying SR.UnexpectedToken. This is the generic 'not a parseable markup extension' error.
Solutions
- Check the markup-extension string for balanced braces and valid 'Name Args' syntax
- Escape literal strings that start with '{' using the '{}' prefix
- Validate the extension name exists (e.g. StaticResource, Binding)
- Catch XamlParseException and log the tokenizer line/column info
Example fix
// before
button.Content = "{CustomThing}"; // malformed
// after
button.Content = "{StaticResource myThing}"; // or escape: "{}{CustomThing}" Defensive patterns
Strategy: validation
Validate before calling
bool LooksLikeMarkupExtension(string s)
{
if (s == null || !s.TrimStart().StartsWith("{")) return true; // literal, fine
string t = s.Trim();
if (t == "{" || t == "{}") return true; // escape sequence / empty
return t.EndsWith("}") && t.Count(c => c=='{') == t.Count(c => c=='}');
} Try / catch
try { nodes = parser.Parse(meString); }
catch (XamlParseException ex) { /* inspect ex.Message for the broken-rule text */ } Prevention
- Escape literal '{' values with '{}' prefix
- Validate brace balance before parsing untrusted input
- Keep extension strings minimal and machine-generated where possible
When it happens
Trigger: Passing a string starting with '{' that does not match the markup-extension grammar: '{' alone, '{}', '{Foo' with unterminated syntax, or a token sequence violating the ME rules.
Common situations: Dynamically built attribute values with malformed curly syntax, strings intended as literals needing {} escape, truncation during serialization.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- SR.InvalidClosingBracketCharacers
- SR.MalformedBracketCharacters
- SR.MalformedPropertyName
- SR.QuoteCharactersOutOfPlace
- SR.UnclosedQuote
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a80ab145eb76549e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/MePullParser.cs:68
// PositionalArgs ::= (Value (',' PositionalArgs)?) | NamedArg
// Value ::= STRING | QUOTEDMARKUPEXTENSION |MarkupExtension
public IEnumerable<XamlNode> Parse(string text, int lineNumber, int linePosition)
{
_tokenizer = new MeScanner(_context, text, lineNumber, linePosition);
_originalText = text;
Found f = new Found();
NextToken();
foreach (XamlNode node in P_MarkupExtension(f))
{
yield return node;
}
if (!f.found)
{
string brokenRule = _brokenRule;
_brokenRule = null;
throw new XamlParseException(_tokenizer, brokenRule);
}
if (_tokenizer.Token != MeTokenType.None)
{
throw new XamlParseException(_tokenizer, SR.UnexpectedTokenAfterME);
}
if (_tokenizer.HasTrailingWhitespace)
{
throw new XamlParseException(_tokenizer, SR.WhitespaceAfterME);
}
}
private void SetBrokenRuleString(string ruleString)
{
if (string.IsNullOrEmpty(_brokenRule))
{
_brokenRule = SR.Format(SR.UnexpectedToken,View on GitHub (pinned to 81131a70a4)