dotnet/wpf · error · FormatException

FormatException(error from XamlTypeName.ParseList)

Error message

FormatException(error from XamlTypeName.ParseList)

What it means

XamlTypeName.ParseList parses a comma-separated list of XAML type names; when any element fails the grammar check, ParseListInternal returns null and the public ParseList throws a FormatException carrying the parser's error message. It exists so batch parsing fails loudly rather than silently returning a partial list.

Solutions

  1. Fix the offending element in the list so every comma-separated token is a valid XAML type name.
  2. Switch to XamlTypeName.TryParseList to get the error message without an exception.
  3. Validate each element individually with TryParse to pinpoint which entry is bad.
  4. Ensure prefixes used in the list are registered in the IXamlNamespaceResolver.

Example fix

// before
var list = XamlTypeName.ParseList("x:String,,x:Int32", ns); // FormatException (empty element)
// after
if (XamlTypeName.TryParseList("x:String,x:Int32", ns, out var parsed, out var error)) { /* use parsed */ }
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(typeNameList)) throw new ArgumentException("List is empty");
if (!XamlTypeName.TryParseList(typeNameList, nsResolver, out var list, out var error))
    throw new FormatException($"Invalid XAML type name list: {error}");

Type guard

bool IsValidXamlTypeList(string s, IXamlNamespaceResolver ns) =>
    !string.IsNullOrWhiteSpace(s) && XamlTypeName.TryParseList(s, ns, out _, out _);

Try / catch

try { var list = XamlTypeName.ParseList(typeNameList, nsResolver); }
catch (FormatException ex) { /* report which list failed; fall back to per-item TryParse for diagnostics */ }

Prevention

When it happens

Trigger: Calling XamlTypeName.ParseList(typeNameList, namespaceResolver) where the comma-delimited string contains any invalid entry: empty element between commas, malformed name, or unresolvable prefix.

Common situations: Parsing generic type argument lists (e.g. 'x:String,x:Int32') where one element was edited or truncated; splitting input on the wrong delimiter and passing fragments; user input in designer/serialization tooling.

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/ded84a9af41065b8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs:116

            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;
        }

        public static bool TryParse(string typeName, IXamlNamespaceResolver namespaceResolver,
            out XamlTypeName result)
        {
            ArgumentNullException.ThrowIfNull(typeName);
            ArgumentNullException.ThrowIfNull(namespaceResolver);

            result = ParseInternal(typeName, namespaceResolver.GetNamespace, out _);
            return (result is not null);
        }

        public static bool TryParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver,
            out IList<XamlTypeName> result)
        {

View on GitHub (pinned to 81131a70a4)