dotnet/wpf · error · InvalidOperationException

SR.CannotParseId

Error message

SR.CannotParseId

What it means

FindAnnotationIds throws InvalidOperationException (SR.CannotParseId, wrapping a FormatException) when an Annotation element's Id attribute exists but is not a parseable Guid (XmlConvert.ToGuid fails).

Solutions

  1. Correct the Id values in the store XML to valid Guids (Guid.TryParse each before loading).
  2. Regenerate the annotation store, assigning Guid.NewGuid()-based Ids during migration.
  3. Pre-validate the file: parse the XML, run Guid.TryParse on every Id attribute, fix failures before handing the stream to XmlStreamStore.
  4. Restore from backup if the corruption is unexplained.

Example fix

// before (store XML)
<Annotation Id="my-annotation-1" ...>

// after
<Annotation Id="d4b1c3a2-2222-4aaa-8ddd-1234567890ab" ...>
Defensive patterns

Strategy: validation

Validate before calling

foreach (var id in doc.Descendants().Where(e => e.Name.LocalName == "Annotation").Select(e => (string)e.Attribute("Id")))
    if (!Guid.TryParse(id, out _)) throw new FormatException($"Invalid annotation Id: {id}");

Type guard

bool IsValidAnnotationId(string id) => Guid.TryParse(id, out _);

Try / catch

try { return store.GetAnnotationsByIds(ids); }
catch (InvalidOperationException ex) when (ex.InnerException is FormatException) { /* quarantine bad store file */ }

Prevention

When it happens

Trigger: Loading an annotation stream whose <Annotation Id="..."> values are malformed - not in the 32-digit Guid form (e.g., arbitrary strings, empty braces, wrong length); called via GetAnnotations/GetAnnotationsByIds on such a stream.

Common situations: Hand-written or tool-generated annotation XML with fake Ids like "abc123"; Ids serialized with different formats (e.g., with extra braces is OK, but truncated or hex Ids are not); data migrated from another annotation system without normalizing Ids to Guids.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs:575

                if (iterator != null && iterator.Count > 0)
                {
                    retObj = new List<Guid>(iterator.Count);
                    foreach (XPathNavigator node in iterator)
                    {
                        string nodeId = node.GetAttribute("Id", "");
                        if (String.IsNullOrEmpty(nodeId))
                        {
                            throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.Id, AnnotationXmlConstants.Elements.Annotation));
                        }

                        try
                        {
                            annId = XmlConvert.ToGuid(nodeId);
                        }
                        catch (FormatException fe)
                        {
                            throw new InvalidOperationException(SR.CannotParseId, fe);
                        }

                        retObj.Add(annId);
                    }
                }
                else
                {
                    retObj = new List<Guid>(0);
                }
            }

            return retObj;
        }

        /// <summary>
        ///     Used as AuthorChanged event handler for all annotations
        ///     handed out by the map.
        /// </summary>

View on GitHub (pinned to 81131a70a4)