dotnet/wpf · error · XmlException

SR.Format(SR.InvalidXmlContent…

Error message

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

What it means

AnnotationResource.ReadXml throws XmlException when the <Resource> element contains XML content that violates the annotation schema — specifically, a non-XmlElement child (such as plain text) directly inside the Resource element. Resource elements may only contain XmlElement children (e.g. <Cargos>, <Contents> child elements). Free-form text nodes inside Resource are schema violations.

Solutions

  1. Fix the annotation XML so <Resource> contains only XmlElement children (no bare text nodes).
  2. Remove stray text/CDATA content directly inside <Resource> elements.
  3. Regenerate the annotation store from the application rather than editing the XML by hand.
  4. Validate annotation files against the annotations schema before loading them.

Example fix

// before
<Resource Name="note">plain text here</Resource>
// after
<Resource Name="note"><Cargos><Cargo>plain text here</Cargo></Cargos></Resource>
Defensive patterns

Strategy: try-catch

Validate before calling

// Before loading, sanity-check the XML:
foreach (var resource in doc.Descendants(ns + "Resource"))
    if (resource.Nodes().Any(n => n is XText t && !string.IsNullOrWhiteSpace(t.Value)))
        throw new InvalidDataException("Resource contains non-element text");

Type guard

bool IsSchemaValidResource(XElement r) => !r.Nodes().Any(n => n is XText);

Try / catch

try { using var store = new XmlStreamStore(stream); store.LoadAnnotations(...); }
catch (XmlException ex) { log(ex); /* repair or regenerate annotation file */ }

Prevention

When it happens

Trigger: Deserializing an annotations XML stream (XmlStreamStore) whose <Resource> element contains raw text or CDATA as a direct child, e.g. <Resource Name="x">some text</Resource> or mixed text content between child elements.

Common situations: Hand-edited annotation XML files; annotation files produced by an older or custom store version with a different schema; corrupted or merged annotation streams; third-party tools writing annotation XML incorrectly.

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

Appendix: source

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

                    {
                        ContentLocatorBase locator = (ContentLocatorBase)LocatorGroupSerializer.Deserialize(reader);
                        InternalLocators.Add(locator);
                    }
                    else if (AnnotationXmlConstants.Elements.ContentLocator == reader.LocalName)
                    {
                        ContentLocatorBase locator = (ContentLocatorBase)ListSerializer.Deserialize(reader);
                        InternalLocators.Add(locator);
                    }
                    else if (XmlNodeType.Element == reader.NodeType)
                    {
                        XmlElement element = doc.ReadNode(reader) as XmlElement;
                        InternalContents.Add(element);
                    }
                    else
                    {
                        // The resource must contain a non-XmlElement child such as plain
                        // text which is not part of the schema.
                        throw new XmlException(SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.Resource));
                    }
                }
            }

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

        #endregion IXmlSerializable Implementation

        #endregion Public Methods

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

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

View on GitHub (pinned to 81131a70a4)