dotnet/wpf · error · XmlException

SR.Format(SR.UnexpectedAttribute, reader.LocalName…

Error message

SR.Format(SR.UnexpectedAttribute, reader.LocalName, AnnotationXmlConstants.Elements.Resource)

What it means

AnnotationResource.ReadAttributes throws XmlException when a <Resource> element carries an attribute whose local name is not recognized (only Id and Name are allowed) and the attribute is not a namespace declaration. This enforces the annotations schema during deserialization via XmlStreamStore.

Solutions

  1. Remove unknown attributes from <Resource> elements, keeping only Id and Name.
  2. If extra metadata is needed, encode it as child Cargo/Content elements per the schema instead of attributes.
  3. Identify which tool wrote the extra attributes and fix its serializer.
  4. Preprocess/normalize annotation XML files before loading them into XmlStreamStore.

Example fix

// before
<Resource Id="6a1f..." Name="note" Type="text">
// after
<Resource Id="6a1f..." Name="note">
Defensive patterns

Strategy: try-catch

Validate before calling

var allowed = new[] { "Id", "Name" };
foreach (var a in resourceEl.Attributes())
    if (!allowed.Contains(a.Name.LocalName) && !a.IsNamespaceDeclaration)
        throw new InvalidDataException($"Unexpected attribute {a.Name.LocalName}");

Type guard

bool HasOnlyKnownAttributes(XElement r) =>
    r.Attributes().All(a =>
        a.IsNamespaceDeclaration || a.Name.LocalName is "Id" or "Name");

Try / catch

try { store = new XmlStreamStore(stream); }
catch (XmlException ex) { log(ex); sanitizeAnnotationFile(path); reload(); }

Prevention

When it happens

Trigger: Loading an annotation XML stream where a <Resource> element has an unknown attribute, e.g. <Resource Id="..." Type="foo">, that is also not a namespace declaration (not in the xmlns family).

Common situations: Annotation XML extended by other applications with extra attributes; schema drift between store versions; hand-edited annotation files; round-tripping annotations through systems that add metadata attributes.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationResource.cs:455

                // Skip null values - they will be treated the same as if
                // they weren't specified at all
                if (value == null)
                    continue;

                switch (reader.LocalName)
                {
                    case AnnotationXmlConstants.Attributes.Id:
                        tempId = XmlConvert.ToGuid(value);
                        break;

                    case AnnotationXmlConstants.Attributes.ResourceName:
                        _name = value;
                        break;

                    default:
                        if (!Annotation.IsNamespaceDeclaration(reader))
                            throw new XmlException(SR.Format(SR.UnexpectedAttribute, reader.LocalName, AnnotationXmlConstants.Elements.Resource));
                        break;
                }
            }

            if (Guid.Empty.Equals(tempId))
            {
                throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.Id, AnnotationXmlConstants.Elements.Resource));
            }

            _id = tempId;

            // Name attribute is optional, so no need to check it

            // Move back to the parent "Resource" element
            reader.MoveToContent();
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)