dotnet/wpf · error · ArgumentException

ArgumentException(SR.Format(SR.MemberHasInvalidXamlName…

Error message

ArgumentException(SR.Format(SR.MemberHasInvalidXamlName, property.Name), nameof(property))

What it means

WriteStartMember validates that the XamlMember's Name is a valid XAML name; if property.IsNameValid is false it throws ArgumentException including the member's name. Member names become XML attribute/element names, so illegal characters are rejected up front.

Solutions

  1. Correct the member's Name to a valid XAML identifier before writing.
  2. Check property.IsNameValid before the call and handle invalid members separately.
  3. Use a properly registered attachable-property definition instead of hand-built member names.

Example fix

// before
writer.WriteStartMember(new XamlMember("My Property", type, false)); // ArgumentException (space)
// after
writer.WriteStartMember(new XamlMember("MyProperty", type, false));
Defensive patterns

Strategy: validation

Validate before calling

if (property is null) throw new ArgumentNullException(nameof(property));
if (!property.IsNameValid) throw new ArgumentException($"Member '{property.Name}' has an invalid XAML name", nameof(property));
writer.WriteStartMember(property);

Type guard

bool IsWritableMember(XamlMember m) => m is not null && m.IsNameValid;

Try / catch

try { writer.WriteStartMember(property); }
catch (ArgumentException ex) { /* fix member name or skip member */ }

Prevention

When it happens

Trigger: Calling xamlXmlWriter.WriteStartMember(property) with a XamlMember whose Name contains characters invalid for XAML/XML names (spaces, invalid leading chars, etc.).

Common situations: Manually constructed XamlMember instances with ad-hoc names; attached-property names built by string concatenation; members derived from CLR names that were renamed or generated with characters invalid in XML names.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9aceb24b6edf06ae. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:191

                WriteTypeArguments(type);
            }
        }

        public override void WriteEndObject()
        {
            CheckIsDisposed();
            currentState.WriteEndObject(this);
        }

        public override void WriteStartMember(XamlMember property)
        {
            CheckIsDisposed();

            ArgumentNullException.ThrowIfNull(property);

            if (!property.IsNameValid)
            {
                throw new ArgumentException(SR.Format(SR.MemberHasInvalidXamlName, property.Name), nameof(property));
            }

            currentState.WriteStartMember(this, property);
        }

        public override void WriteEndMember()
        {
            CheckIsDisposed();
            currentState.WriteEndMember(this);
        }

        public override void WriteValue(object value)
        {
            CheckIsDisposed();
            if (value is null)
            {
                WriteStartObject(XamlLanguage.Null);
                WriteEndObject();

View on GitHub (pinned to 81131a70a4)