dotnet/wpf · error · XmlException

SR.Format(SR.InvalidXmlContent…

Error message

SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.ContentLocatorGroup)

What it means

ContentLocatorGroup.ReadXml throws this XmlException when the XML reader encounters a child element inside <ContentLocatorGroup> that is not a valid ContentLocatorBase element (ContentLocator or ContentLocatorPart) or text. The file's content violates the annotations XML schema.

Solutions

  1. Fix the XML so ContentLocatorGroup contains only ContentLocator or ContentLocatorPart child elements.
  2. Regenerate the annotation store instead of hand-editing it.
  3. Catch XmlException during store load and fall back to a fresh store or skip the malformed annotation.

Example fix

// before (invalid)
<ContentLocatorGroup><Foo/></ContentLocatorGroup>
// after (valid)
<ContentLocatorGroup><ContentLocator><ContentLocatorPart PartType="..."/></ContentLocator></ContentLocatorGroup>
Defensive patterns

Strategy: try-catch

Validate before calling

XDocument doc = XDocument.Parse(xml);
bool valid = doc.Root?.Elements().All(e => e.Name.LocalName is "ContentLocator" or "ContentLocatorPart") == true;

Type guard

bool IsValidLocatorGroup(XElement g) => g.Elements().All(e => e.Name.LocalName is "ContentLocator" or "ContentLocatorPart");

Try / catch

try { store.Load(stream); } catch (XmlException ex) { log.Warn(ex, "Corrupt annotation store"); store = CreateFreshStore(); }

Prevention

When it happens

Trigger: Loading an annotation store XML whose ContentLocatorGroup contains unknown/misspelled child elements, elements from a foreign namespace, or hand-edited markup that does not match the schema.

Common situations: Manually editing annotation store files; stores produced by third-party or newer/older tooling with extra elements; corrupted or schema-invalid XML persisted by a custom XmlAnnotationStore.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/LocatorGroup.cs:145

            if (!reader.IsEmptyElement)
            {
                reader.Read();  // Reads the start of the "ContentLocatorGroup" element

                // Consume everything inside of the ContentLocatorGroup tag.
                while (!(AnnotationXmlConstants.Elements.ContentLocatorGroup == reader.LocalName && XmlNodeType.EndElement == reader.NodeType))
                {
                    // If a child node is a <ContentLocatorBase>, deserialize a ContentLocatorBase
                    if (AnnotationXmlConstants.Elements.ContentLocator == reader.LocalName)
                    {
                        ContentLocator locator = (ContentLocator)AnnotationResource.ListSerializer.Deserialize(reader);                        
                        _locators.Add(locator);
                    }
                    else
                    {
                        // The ContentLocatorGroup contains a child that is not a ContentLocatorBase or 
                        // text.  This isn't valid in the schema so we throw.
                        throw new XmlException(SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.ContentLocatorGroup));
                    }
                }
            }

            reader.Read();   // Reads the end of the "ContentLocatorGroup" element (or whole element if empty)
        }

        #endregion IXmlSerializable Implementation

        #endregion Public Methods

        //------------------------------------------------------
        //
        //  Public Operators
        //
        //------------------------------------------------------

        //------------------------------------------------------

View on GitHub (pinned to 81131a70a4)