dotnet/wpf · error · XamlParseException

error

Error message

error

What it means

XamlParseException thrown from ResolveXamlType when XamlTypeName.ParseInternal fails to parse the qualified name string into a XamlTypeName. The library throws it because the qName string is not a syntactically valid XAML type name (e.g. missing namespace prefix or malformed syntax), and the parse error message is carried into the exception.

Solutions

  1. Fix the qualified name string to include a valid prefix declared in the current namespace context, e.g. 'sys:String' with xmlns:sys declared
  2. Register the needed XmlnsDictionary/prefix mapping before resolving so the prefix parses and resolves
  3. Wrap type-name resolution in try/catch on XamlParseException and inspect the parse error message
  4. Validate the name with XamlTypeName.Parse on a test parser before passing it to the resolver

Example fix

// before
var type = context.ResolveXamlType("String"); // no prefix declared -> parse fails
// after
var type = context.ResolveXamlType("sys:String"); // with xmlns:sys="clr-namespace:System;assembly=mscorlib" declared
Defensive patterns

Strategy: try-catch

Validate before calling

// validate before resolving
if (string.IsNullOrWhiteSpace(qName) || (!qName.Contains(':') && prefixRequired))
    throw new ArgumentException("Qualified XAML type name must include a declared prefix, e.g. 'sys:String'");

Try / catch

try { var type = context.ResolveXamlType(qName, false); }
catch (XamlParseException ex) { log.Error("Invalid XAML type name '{0}': {1}", qName, ex.Message); }

Prevention

When it happens

Trigger: Calling ResolveXamlType (directly or via XamlType resolution from a XAML reader) with a qName string that fails XamlTypeName.ParseInternal — for example an empty string, a bare local name with no prefix where a prefix is required, unbalanced parentheses in markup-extension style names, or an unknown/empty prefix when no default namespace applies.

Common situations: Hand-built XAML node streams feeding a XamlXmlWriter/XamlObjectWriter with mistyped x:Type strings; programmatically resolving type names where a namespace prefix was never registered via a namespace declaration; refactored XML where the xmlns declaration was removed but element type names still reference the prefix.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlContext.cs:318

                    _resolvePrefixCachedDelegate = new Func<string, string>(FindNamespaceByPrefix);
                }

                return _resolvePrefixCachedDelegate;
            }
        }

        private string ResolveXamlNameNS(XamlName name)
        {
            return name.Namespace ?? FindNamespaceByPrefix(name.Prefix);
        }

        internal XamlType ResolveXamlType(string qName, bool skipVisibilityCheck)
        {
            string error;
            XamlTypeName typeName = XamlTypeName.ParseInternal(qName, ResolvePrefixCachedDelegate, out error);
            if (typeName is null)
            {
                throw new XamlParseException(error);
            }

            return GetXamlType(typeName, false, skipVisibilityCheck);
        }

        internal XamlMember ResolveDirectiveProperty(string xamlNS, string name)
        {
            if (xamlNS is not null)
            {
                return SchemaContext.GetXamlDirective(xamlNS, name);
            }

            return null;
        }

        // Only pass rootObjectType if the member is being looked up on the root object
        internal virtual bool IsVisible(XamlMember member, XamlType rootObjectType)
        {

View on GitHub (pinned to 81131a70a4)