dotnet/wpf · error · ArgumentException
ArgumentException(SR.Format(SR.TypeHasInvalidXamlName…
Error message
ArgumentException(SR.Format(SR.TypeHasInvalidXamlName, type.Name), nameof(type))
What it means
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.
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.
Example fix
// before
writer.WriteStartObject(new XamlSchemaContext().GetXamlType(typeof(Foo)) with bad name); // ArgumentException
// after
if (type.IsNameValid) { writer.WriteStartObject(type); } else { /* sanitize or map type */ } Defensive patterns
Strategy: validation
Validate before calling
if (type is null) throw new ArgumentNullException(nameof(type));
if (!type.IsNameValid) throw new ArgumentException($"Type '{type.Name}' has an invalid XAML name", nameof(type));
writer.WriteStartObject(type); Type guard
bool IsWritableType(XamlType t) => t is not null && t.IsNameValid;
Try / catch
try { writer.WriteStartObject(type); }
catch (ArgumentException ex) { /* sanitize the type name or map to a valid XAML type */ } Prevention
- 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.
When it happens
Trigger: Calling xamlXmlWriter.WriteStartObject(type) with a XamlType whose Name contains invalid characters (spaces, leading digits, punctuation) or is otherwise not a valid XAML name.
Common situations: 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.
Understand the failure class
Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.
Related errors
- ArgumentException(SR.Format(SR.MemberHasInvalidXamlName…
- Bracket characters cannot be alpha-numeric or whitespace.
- Bracket characters cannot be one of the following: '=' …
- Opening bracket character cannot be the same as closing…
- SR.Format(SR.InvalidPropertyValue, value…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/1f2908130cf36344.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:166
Frame frame = namespaceScopes.Peek();
if (frame.AllocatingNodeType == XamlNodeType.StartMember)
{
type = frame.Member.Type;
}
currentState.WriteObject(this, type, true);
}
public override void WriteStartObject(XamlType type)
{
CheckIsDisposed();
ArgumentNullException.ThrowIfNull(type);
if (!type.IsNameValid)
{
throw new ArgumentException(SR.Format(SR.TypeHasInvalidXamlName, type.Name), nameof(type));
}
currentState.WriteObject(this, type, false);
if (type.TypeArguments is not null)
{
WriteTypeArguments(type);
}
}
public override void WriteEndObject()
{
CheckIsDisposed();
currentState.WriteEndObject(this);
}
public override void WriteStartMember(XamlMember property)
{View on GitHub (pinned to 81131a70a4)