dotnet/wpf · error · XmlException

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

Error message

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

What it means

When reading an <Item> element inside a ContentLocatorPart, only the expected Name/Value attributes (and namespace declarations) are allowed. Any other attribute produces XmlException(SR.UnexpectedAttribute, attributeName, "Item"). This enforces the strict annotation XML format.

Solutions

  1. Remove unknown attributes from Item elements, keeping only Name and Value (plus xmlns declarations).
  2. Regenerate the annotation stream with a compatible writer.
  3. Pre-validate the XML against the annotation schema before loading.

Example fix

// before
<Item Name="key" Value="val" Extra="oops"/>
// after
<Item Name="key" Value="val"/>
Defensive patterns

Strategy: validation

Validate before calling

foreach (var item in doc.Descendants("Item"))
    foreach (var attr in item.Attributes())
        if (attr.Name.LocalName is not ("Name" or "Value") && !attr.IsNamespaceDeclaration)
            throw new InvalidDataException($"Unexpected attribute {attr.Name.LocalName} on Item");

Try / catch

try { LoadAnnotations(stream); }
catch (XmlException ex) when (ex.Message.Contains("attribute")) { /* quarantine the bad annotation file and log */ }

Prevention

When it happens

Trigger: Deserializing an annotations stream where an Item element carries an unknown attribute, e.g. <Item Name="k" Value="v" Extra="x"/>.

Common situations: Annotation files edited or generated by external tools adding metadata attributes; schema evolution between framework versions; copy-paste of similar markup from other XML formats.

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/1a487e4e9cafa2b7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/LocatorPartList.cs:240

                        {
                            if (AnnotationXmlConstants.Elements.Item == reader.LocalName && reader.NamespaceURI == AnnotationXmlConstants.Namespaces.CoreSchemaNamespace)
                            {
                                string name = null;
                                string value = null;

                                while (reader.MoveToNextAttribute())
                                {
                                    switch (reader.LocalName)
                                    {
                                        case AnnotationXmlConstants.Attributes.ItemName:
                                            name = reader.Value;
                                            break;
                                        case AnnotationXmlConstants.Attributes.ItemValue:
                                            value = reader.Value;
                                            break;
                                        default:
                                            if (!Annotation.IsNamespaceDeclaration(reader))
                                                throw new XmlException(SR.Format(SR.UnexpectedAttribute, reader.LocalName, AnnotationXmlConstants.Elements.Item));
                                            break;
                                    }
                                }

                                if (name == null)
                                {
                                    throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.ItemName, AnnotationXmlConstants.Elements.Item));
                                }
                                if (value == null)
                                {
                                    throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.ItemValue, AnnotationXmlConstants.Elements.Item));
                                }

                                reader.MoveToContent();

                                part.NameValuePairs.Add(name, value);

                                bool isEmpty = reader.IsEmptyElement;

View on GitHub (pinned to 81131a70a4)