{"record":{"id":"30f29e1d0026c721","repo":"dotnet/wpf","slug":"formatexception-error-from-xamltypename-parse","errorCode":null,"errorMessage":"FormatException(error from XamlTypeName.Parse)","messagePattern":"FormatException\\(error from XamlTypeName\\.Parse\\)","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs","lineNumber":101,"sourceCode":"        }\n\n        public static string ToString(IList<XamlTypeName> typeNameList, INamespacePrefixLookup prefixLookup)\n        {\n            ArgumentNullException.ThrowIfNull(typeNameList);\n            ArgumentNullException.ThrowIfNull(prefixLookup);\n            return ConvertListToStringInternal(typeNameList, prefixLookup.LookupPrefix);\n        }\n\n        public static XamlTypeName Parse(string typeName, IXamlNamespaceResolver namespaceResolver)\n        {\n            ArgumentNullException.ThrowIfNull(typeName);\n            ArgumentNullException.ThrowIfNull(namespaceResolver);\n\n            string error;\n            XamlTypeName result = ParseInternal(typeName, namespaceResolver.GetNamespace, out error);\n            if (result is null)\n            {\n                throw new FormatException(error);\n            }\n\n            return result;\n        }\n\n        public static IList<XamlTypeName> ParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver)\n        {\n            ArgumentNullException.ThrowIfNull(typeNameList);\n            ArgumentNullException.ThrowIfNull(namespaceResolver);\n\n            string error;\n            IList<XamlTypeName> result = ParseListInternal(typeNameList, namespaceResolver.GetNamespace, out error);\n            if (result is null)\n            {\n                throw new FormatException(error);\n            }\n\n            return result;","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs#L83-L119","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar name = XamlTypeName.Parse(\"sys:Int32,\"); // FormatException\n// after\nif (!XamlTypeName.TryParse(\"sys:Int32\", nsResolver, out var xtn, out var error))\n{\n    Console.WriteLine($\"Invalid type name: {error}\");\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(typeName)) throw new ArgumentException(\"Type name is empty\");\n// prefer:\nif (!XamlTypeName.TryParse(typeName, nsResolver, out var xtn, out var error))\n    throw new FormatException($\"Invalid XAML type name '{typeName}': {error}\");","typeGuard":"bool IsValidXamlTypeName(string s, IXamlNamespaceResolver ns) =>\n    !string.IsNullOrWhiteSpace(s) && XamlTypeName.TryParse(s, ns, out _, out _);","tryCatchPattern":"try { var t = XamlTypeName.Parse(typeName, nsResolver); }\ncatch (FormatException ex) { /* log ex.Message, surface validation error to user */ }","preventionTips":["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."],"tags":["xaml","parsing","format"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}