dotnet/wpf · error · MappingParseError
MappingParseError(_scanner.Start, MappingScanner.Ident…
Error message
MappingParseError(_scanner.Start, MappingScanner.Ident, _token, ident)
What it means
A MappingParseError thrown by XamlReaderHelper's private Error(ident) helper when the mapping scanner fails to parse an identifier in a mapping/prefix declaration (e.g. xmlns:prefix='clr-namespace:...' processing instructions). The XAML loader could not read the expected identifier token while processing mapping directives, so it aborts parsing.
Solutions
- Fix the mapping/namespace declaration in the XAML so it contains a valid identifier (e.g. clr-namespace:MyNs;assembly=MyAssembly).
- Check the exact position reported by the error (scanner Start/token) to find the malformed character or missing token.
- If XAML is generated at runtime, validate/escape namespace strings before embedding them in the XAML.
- If using the Mapping PI, ensure required parts (ClrNamespace, Assembly) are present and well-formed.
Example fix
// before <?Mapping XmlNamespace='ui' ClrNamespace='My.App.UI;' ?> // after <?Mapping XmlNamespace='ui' ClrNamespace='My.App.UI' Assembly='My.App' ?>
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(xaml) || !System.Text.RegularExpressions.Regex.IsMatch(xaml, "clr-namespace:[A-Za-z_][A-Za-z0-9_.]*")) throw new FormatException("Mapping declaration is malformed; expected clr-namespace:Name;assembly=Asm"); Try / catch
try { return (T)XamlReader.Parse(xaml); } catch (XamlParseException ex) { log.Error($"Mapping error at {ex.LinePosition}: {ex.Message}"); throw; } Prevention
- Validate mapping PIs and xmlns declarations before loading.
- Use fully qualified clr-namespace with assembly= clause.
- Test generated XAML against the real parser in CI.
When it happens
Trigger: Calling XamlReader.Load/XamlReader parse flows on XAML whose mapping PI (<?Mapping ...?>) or clr-namespace/xmlns declarations contain a malformed or missing identifier where the MappingScanner expected an Ident token.
Common situations: Hand-written or generated XAML with typos in clr-namespace names, missing assembly= parts, stray characters in xmlns mappings, or programmatically constructed XAML strings produced by tools emitting invalid mapping syntax.
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.Format(SR.LengthFormatError, span.ToString())
- SR.Format(SR.NonWhiteSpaceInAddText, s)
- SR.GlyphsClusterMisplacedSeparator
- SR.GlyphsClusterNoMatchingBracket
- SR.GlyphsIndexRequiredIfNoUnicode
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/34084993afec6add.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReaderHelper.cs:2713
Expected = expected;
Received = received;
}
public MappingParseError(int offset, char expected, char received, string ident)
: this(offset, expected, received)
{
Ident = ident;
}
}
private void Error(char token)
{
throw new MappingParseError(_scanner.Start, token, _token);
}
private void Error(string ident)
{
throw new MappingParseError(_scanner.Start, MappingScanner.Ident, _token, ident);
}
}
#endregion
private string ResolveAttributeNamespaceURI(string prefix, string name, string parentURI)
{
string attribNamespaceURI;
if(!String.IsNullOrEmpty(prefix))
{
attribNamespaceURI = XmlReader.LookupNamespace(prefix);
}
else
{
// if the prefix was "" then
// 1) normal properties resolve to the parent Tag namespace.
// 2) Attached properties resolve to the "" default namespace.
#if !NETFX
if (!name.Contains('.'))View on GitHub (pinned to 81131a70a4)