dotnet/wpf · error · XmlException

SR.Format(SR.RequiredAttributeMissing…

Error message

SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.Id, AnnotationXmlConstants.Elements.Resource)

What it means

AnnotationResource.ReadAttributes throws XmlException when a <Resource> element in the annotation XML lacks the required Id attribute. Every resource in the annotations schema must carry a Guid Id; without it the resource cannot be identified or attached to an annotation. The check fires when parsing leaves tempId at Guid.Empty.

Solutions

  1. Add a unique Guid Id attribute to each <Resource> element in the annotation file.
  2. Ensure Id attribute values are well-formed Guids (parseable by Guid.Parse).
  3. Restore the annotation file from a backup or regenerate the store.
  4. Validate annotation XML against the schema before loading.

Example fix

// before
<Resource Name="note">
// after
<Resource Id="3f2504e0-4f89-11d3-9a0c-0305e82c3301" Name="note">
Defensive patterns

Strategy: try-catch

Validate before calling

foreach (var resourceEl in doc.Descendants(ns + "Resource"))
    if ((string)resourceEl.Attribute("Id") == null)
        resourceEl.SetAttributeValue("Id", Guid.NewGuid());

Type guard

bool HasRequiredId(XElement r) => Guid.TryParse((string?)r.Attribute("Id"), out _);

Try / catch

try { store = new XmlStreamStore(stream); annotations = store.LoadAnnotations(...); }
catch (XmlException ex) { log(ex); restoreFromBackup(); }

Prevention

When it happens

Trigger: Deserializing annotation XML from XmlStreamStore where a <Resource> element has no Id attribute (or an Id that failed to parse to a Guid, yielding Guid.Empty).

Common situations: Truncated or hand-edited annotation files; annotation streams written by tools that omit Id; corrupted XML after partial writes; copy-paste edits that dropped the attribute.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

                {
                    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>
        /// Listens for change events from the list of locators.  Fires a change event
        /// for this resource when an event is received.
        /// </summary>
        private void OnLocatorsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            FireResourceChanged("Locators");
        }

View on GitHub (pinned to 81131a70a4)