dotnet/wpf · error · XmlException

SR.Format(SR.RequiredAttributeMissing…

Error message

SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.ItemName, AnnotationXmlConstants.Elements.Item)

What it means

Each <Item> element in a ContentLocatorPart must carry a Name attribute; when it is absent, ReadXml throws XmlException(SR.RequiredAttributeMissing, "Name", "Item"). Name/Value pairs are the key-value payload of the locator part, so a missing key cannot be represented.

Solutions

  1. Add a Name attribute to every Item element.
  2. Fix the custom writer that produces Item elements so it always emits Name.
  3. Validate the XML against the annotation schema before loading the stream.

Example fix

// before
<Item Value="v"/>
// after
<Item Name="key" Value="v"/>
Defensive patterns

Strategy: validation

Validate before calling

foreach (var item in doc.Descendants("Item"))
    if ((string?)item.Attribute("Name") is null)
        throw new InvalidDataException("Item is missing required Name attribute");

Try / catch

try { LoadAnnotations(stream); }
catch (XmlException ex) when (ex.Message.Contains("Name")) { /* repair the Item elements or skip the file */ }

Prevention

When it happens

Trigger: Deserializing an annotations stream where an Item element has only a Value attribute, e.g. <Item Value="v"/>.

Common situations: External generators omitting the Name attribute; hand-edited annotation files; bugs in custom annotation writers.

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

Appendix: source

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

                                {
                                    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;

                                reader.Read();  // Read the beginning of the complete Item tag

                                if (!isEmpty)
                                {
                                    if (!(XmlNodeType.EndElement == reader.NodeType && AnnotationXmlConstants.Elements.Item == reader.LocalName))
                                    {

View on GitHub (pinned to 81131a70a4)