dotnet/wpf · error · FormatException
FormatException(error from XamlTypeName.Parse)
Error message
FormatException(error from XamlTypeName.Parse)
What it means
XamlTypeName.Parse could not parse the given XAML type name string (e.g. 'prefix:Name' or '{ns}Name') into a XamlTypeName, so it returns null internally and the public Parse method surfaces the parser's error text as a FormatException. This library throws it because the input string is not a well-formed XAML type name according to the grammar (namespace prefix, colon, name, optional type arguments).
Solutions
- Correct the input string to valid XAML type-name syntax ('prefix:TypeName' or '{namespaceUri}TypeName').
- Use XamlTypeName.TryParse (or TryParseList) to validate without exceptions and inspect the out error string.
- Verify the IXamlNamespaceResolver actually maps the prefix used in the string to a namespace.
- If the string came from serialized output, regenerate it via XamlTypeName.ToString instead of manual concatenation.
Example fix
// before
var name = XamlTypeName.Parse("sys:Int32,"); // FormatException
// after
if (!XamlTypeName.TryParse("sys:Int32", nsResolver, out var xtn, out var error))
{
Console.WriteLine($"Invalid type name: {error}");
} Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(typeName)) throw new ArgumentException("Type name is empty");
// prefer:
if (!XamlTypeName.TryParse(typeName, nsResolver, out var xtn, out var error))
throw new FormatException($"Invalid XAML type name '{typeName}': {error}"); Type guard
bool IsValidXamlTypeName(string s, IXamlNamespaceResolver ns) =>
!string.IsNullOrWhiteSpace(s) && XamlTypeName.TryParse(s, ns, out _, out _); Try / catch
try { var t = XamlTypeName.Parse(typeName, nsResolver); }
catch (FormatException ex) { /* log ex.Message, surface validation error to user */ } Prevention
- Always prefer TryParse over Parse when input is user- or config-supplied.
- Keep prefix-to-namespace mappings in sync with the strings you parse.
- Never concatenate type names by hand; round-trip through XamlTypeName.ToString.
When it happens
Trigger: Calling XamlTypeName.Parse(typeName, namespaceResolver) with a malformed string: missing name, unbalanced parentheses in type arguments, unknown/empty prefix that GetNamespace resolves to null, or stray characters.
Common situations: Parsing user-supplied or config-driven type names; hand-constructed strings missing the prefix or using '{'/'}' incorrectly; locale/markup that mangles the syntax; refactoring that changes prefix mappings so the prefix no longer resolves.
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
- FormatException(error from XamlTypeName.ParseList)
- SR.Format(SR.LengthFormatError, span.ToString())
- ' ' is not a valid XAML member name.
- Invalid XAML type-name list string; error message produced…
- MappingParseError(_scanner.Start, MappingScanner.Ident…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/30f29e1d0026c721.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs:101
}
public static string ToString(IList<XamlTypeName> typeNameList, INamespacePrefixLookup prefixLookup)
{
ArgumentNullException.ThrowIfNull(typeNameList);
ArgumentNullException.ThrowIfNull(prefixLookup);
return ConvertListToStringInternal(typeNameList, prefixLookup.LookupPrefix);
}
public static XamlTypeName Parse(string typeName, IXamlNamespaceResolver namespaceResolver)
{
ArgumentNullException.ThrowIfNull(typeName);
ArgumentNullException.ThrowIfNull(namespaceResolver);
string error;
XamlTypeName result = ParseInternal(typeName, namespaceResolver.GetNamespace, out error);
if (result is null)
{
throw new FormatException(error);
}
return result;
}
public static IList<XamlTypeName> ParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver)
{
ArgumentNullException.ThrowIfNull(typeNameList);
ArgumentNullException.ThrowIfNull(namespaceResolver);
string error;
IList<XamlTypeName> result = ParseListInternal(typeNameList, namespaceResolver.GetNamespace, out error);
if (result is null)
{
throw new FormatException(error);
}
return result;View on GitHub (pinned to 81131a70a4)