dotnet/wpf · error · XmlException

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

Error message

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

What it means

Annotation.ReadAttributes rejects any attribute on the <Annotation> element that is not a recognized attribute (Id, TypeName, CreationTime, LastModificationTime) and not a namespace declaration. The library throws XmlException to signal that the annotation XML stream is not valid per the annotations schema.

Solutions

  1. Remove or rename the unexpected attribute on the <Annotation> element so it matches the schema (Id, TypeName, CreationTime, LastModificationTime).
  2. Move custom metadata into a namespace-declared attribute or a child element such as <Cargos> instead of a plain attribute.
  3. Regenerate the annotation XML from a trusted source rather than hand-editing.
  4. If the attribute is a legitimate extension, add the xmlns declaration so Annotation.IsNamespaceDeclaration returns true for it.

Example fix

// before
<Annotation Id="..." Author="me" ...>
// after
<Annotation Id="..." xmlns:ex="http://example.com/annot" ex:Author="me" ...>
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidAnnotationXml(XElement el) => el.Name.LocalName == "Annotation" && el.Attributes().All(a => new[]{"Id","TypeName","CreationTime","LastModificationTime"}.Contains(a.Name.LocalName) || !string.IsNullOrEmpty(a.Name.NamespaceName));

Try / catch

try { using var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)); serializer.Deserialize(ms); } catch (XmlException ex) { log.Error("Invalid annotation XML attribute: " + ex.Message); }

Prevention

When it happens

Trigger: Deserializing an annotation store XML (Annotation.ReadXml / XmlSerializer) that contains an <Annotation> element with an unknown attribute, e.g. a custom attribute or a misspelled attribute name such as 'CreatTime'.

Common situations: Hand-edited annotation files, XML produced by a different annotation tool or newer/older WPF version with extra attributes, or XML transformed through schemas that added 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/07a9915e1f00e318. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Annotation.cs:664

                            ReadOnlySpan<char> @namespace = typeName[segments[0]];
                            ReadOnlySpan<char> name = typeName[segments[1]];
                            if (@namespace.IsEmpty || name.IsEmpty)
                            {
                                // One colon, prefix or suffix is empty string or whitespace
                                throw new FormatException(SR.Format(SR.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName));
                            }
                            _typeName = new XmlQualifiedName(name.ToString(), reader.LookupNamespace(@namespace.ToString()));
                        }
                        else
                        {
                            // More than one colon
                            throw new FormatException(SR.Format(SR.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName));
                        }
                        break;

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

            // Test to see if any required attribute was missing
            if (_id.Equals(Guid.Empty))
            {
                throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.Id, AnnotationXmlConstants.Elements.Annotation));
            }
            if (_created.Equals(DateTime.MinValue))
            {
                throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.CreationTime, AnnotationXmlConstants.Elements.Annotation));
            }
            if (_modified.Equals(DateTime.MinValue))
            {
                throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.LastModificationTime, AnnotationXmlConstants.Elements.Annotation));
            }
            if (_typeName == null)

View on GitHub (pinned to 81131a70a4)