dotnet/wpf · error · XamlParseException

SR.UnclosedQuote

Error message

SR.UnclosedQuote

What it means

MeScanner.ReadString opens a quote at the start of a token and expects it to be closed before the value ends. If ReadString finishes scanning with quoteChar still set (the opening quote was never matched), it throws XamlParseException with SR.UnclosedQuote. System.Xaml throws this because an unterminated quoted string inside a markup extension leaves the token stream incomplete.

Solutions

  1. Add the missing closing quote so each quote inside the braces is paired.
  2. Use the opposite quote character inside the extension than the XML attribute uses (Text="{'Key'}" vs Text='{"Key"}').
  3. Replace problematic inline strings with x:Static or resource references.
  4. Run the file through an XML/XAML editor that highlights unbalanced quotes.

Example fix

<!-- before -->
<TextBlock Text="{StaticResource 'Key}" />

<!-- after -->
<TextBlock Text="{StaticResource 'Key'}" />
Defensive patterns

Strategy: validation

Validate before calling

static bool QuotesClosed(string value) { bool inS=false,inD=false; foreach (var c in value){ if(c=='\'' && !inD) inS=!inS; else if(c=='"' && !inS) inD=!inD; } return !inS && !inD; }

Try / catch

try { return XamlReader.Parse(xaml); } catch (XamlParseException ex) { Log("Unclosed quote in XAML", ex.LineNumber, ex.LinePosition); return null; }

Prevention

When it happens

Trigger: XAML like `Text="{StaticResource 'Key}"` or `Text="{Binding Path="Name"}"`-style mistakes where a quote opens a token in the extension but the matching close quote is missing before the extension terminator.

Common situations: Mixing outer XML attribute quotes with inner extension quotes of the same character; string-built XAML where a quote was dropped during concatenation; localized strings containing quotes pasted into extensions.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/fa2177e93e5dd354. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/MeScanner.cs:512

                        {
                            if (_context.CurrentBracketModeParseParameters?.BracketCharacterStack.Count > 0)
                            {
                                throw new XamlParseException(this, SR.Format(SR.MalformedBracketCharacters, ch.ToString()));
                            }
                        }

                        PushBack();
                        break;  // we are done.
                    }
                }

                atStart = false;
                Advance();
            }

            if (quoteChar != NullChar)
            {
                throw new XamlParseException(this, SR.UnclosedQuote);
            }

            string result = sb.ToString();
            if (!wasQuoted)
            {
                result = result.TrimEnd(KnownStrings.WhitespaceChars);
                result = result.TrimStart(KnownStrings.WhitespaceChars);
            }

            if (_state == StringState.Property)
            {
                _currentParameterName = result;
                _currentSpecialBracketCharacters = GetBracketCharacterForProperty(_currentParameterName);
            }

            return result;
        }

View on GitHub (pinned to 81131a70a4)