{"record":{"id":"1f2908130cf36344","repo":"dotnet/wpf","slug":"argumentexception-sr-format-sr-typehasinvalidxamlname-type","errorCode":null,"errorMessage":"ArgumentException(SR.Format(SR.TypeHasInvalidXamlName, type.Name), nameof(type))","messagePattern":"ArgumentException\\(SR\\.Format\\(SR\\.TypeHasInvalidXamlName, type\\.Name\\), nameof\\(type\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":166,"sourceCode":"            Frame frame = namespaceScopes.Peek();\n\n            if (frame.AllocatingNodeType == XamlNodeType.StartMember)\n            {\n                type = frame.Member.Type;\n            }\n\n            currentState.WriteObject(this, type, true);\n        }\n\n        public override void WriteStartObject(XamlType type)\n        {\n            CheckIsDisposed();\n\n            ArgumentNullException.ThrowIfNull(type);\n\n            if (!type.IsNameValid)\n            {\n                throw new ArgumentException(SR.Format(SR.TypeHasInvalidXamlName, type.Name), nameof(type));\n            }\n\n            currentState.WriteObject(this, type, false);\n\n            if (type.TypeArguments is not null)\n            {\n                WriteTypeArguments(type);\n            }\n        }\n\n        public override void WriteEndObject()\n        {\n            CheckIsDisposed();\n            currentState.WriteEndObject(this);\n        }\n\n        public override void WriteStartMember(XamlMember property)\n        {","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L148-L184","documentation":"WriteStartObject validates that the XamlType's Name is a valid XAML name before writing; if type.IsNameValid is false it throws ArgumentException naming the offending type. XAML names must be valid NCNames, so types with characters like dots-in-wrong-places or other illegal identifiers cannot be written to XML.","triggerScenarios":"Calling xamlXmlWriter.WriteStartObject(type) with a XamlType whose Name contains invalid characters (spaces, leading digits, punctuation) or is otherwise not a valid XAML name.","commonSituations":"Constructing XamlType instances manually with fabricated names; reflection-based type names containing characters invalid in XML names; renamed/mangled types from generated or obfuscated assemblies.","solutions":["Fix or sanitize the type's Name so it is a valid XAML (NCName) identifier before writing.","Map the type to a valid XAML representation (e.g. via a value serializer or different type mapping) instead of writing it directly.","Check type.IsNameValid yourself and handle invalid types before calling WriteStartObject."],"exampleFix":"// before\nwriter.WriteStartObject(new XamlSchemaContext().GetXamlType(typeof(Foo)) with bad name); // ArgumentException\n// after\nif (type.IsNameValid) { writer.WriteStartObject(type); } else { /* sanitize or map type */ }","handlingStrategy":"validation","validationCode":"if (type is null) throw new ArgumentNullException(nameof(type));\nif (!type.IsNameValid) throw new ArgumentException($\"Type '{type.Name}' has an invalid XAML name\", nameof(type));\nwriter.WriteStartObject(type);","typeGuard":"bool IsWritableType(XamlType t) => t is not null && t.IsNameValid;","tryCatchPattern":"try { writer.WriteStartObject(type); }\ncatch (ArgumentException ex) { /* sanitize the type name or map to a valid XAML type */ }","preventionTips":["Check IsNameValid before every WriteStartObject when types come from external sources.","Avoid hand-constructing XamlType names; derive them from real CLR types.","Sanitize generated/obfuscated type names before serialization."],"tags":["xaml","validation","naming"],"backgroundTag":"invalid-identifier-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"}