dotnet/wpf · error · XmlException

SR.InvalidXmlContent

Error message

SR.InvalidXmlContent

What it means

ReadXml throws XmlException with SR.InvalidXmlContent when, inside the Authors collection, it encounters an element that is not the expected StringAuthor element. The annotation XML must strictly follow the Annotations schema; any other element inside Authors is rejected.

Solutions

  1. Ensure every child element inside <Authors> is <StringAuthor> (or the collection is empty)
  2. Regenerate the annotation XML using the WPF AnnotationService so it conforms to the schema
  3. Strip or relocate non-schema elements before loading the XML with ReadXml

Example fix

// before
<Authors><StringAuthor>alice</StringAuthor><CustomAuthor>bob</CustomAuthor></Authors>
// after
<Authors><StringAuthor>alice</StringAuthor><StringAuthor>bob</StringAuthor></Authors>
Defensive patterns

Strategy: validation

Validate before calling

XDocument doc = XDocument.Load(annotationStream);
var bad = doc.Descendants("Authors")
    .SelectMany(a => a.Elements())
    .Where(e => e.Name.LocalName != "StringAuthor")
    .ToList();
if (bad.Any()) throw new InvalidOperationException("Authors contains non-schema elements: " + string.Join(",", bad.Select(b => b.Name.LocalName)));

Try / catch

try
{
    annotation.ReadXml(reader);
}
catch (XmlException ex)
{
    logger.LogError(ex, "Annotation XML has invalid content inside Authors");
    throw new InvalidDataException("Annotation store does not match the annotations schema", ex);
}

Prevention

When it happens

Trigger: Deserializing annotation XML whose <Authors> element contains a child element other than <StringAuthor> (e.g. a custom author element or an added extension element).

Common situations: Hand-edited annotation store XML; XML produced by a different or older schema version; third-party tools injecting author metadata into the Authors collection.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Annotation.cs:325

                                {
                                    AnnotationResource cargo = (AnnotationResource)ResourceSerializer.Deserialize(reader);
                                    _cargos.Add(cargo);
                                }
                            }
                            reader.Read();    // Reads the "Cargos" end tag (or whole tag if it was empty)
                        }
                        else if (AnnotationXmlConstants.Elements.AuthorCollection == reader.LocalName)
                        {
                            CheckForNonNamespaceAttribute(reader, AnnotationXmlConstants.Elements.AuthorCollection);

                            if (!reader.IsEmptyElement)
                            {
                                reader.Read();    // Reads the "Authors" start tag
                                while (!(AnnotationXmlConstants.Elements.AuthorCollection == reader.LocalName && XmlNodeType.EndElement == reader.NodeType))
                                {
                                    if (!(AnnotationXmlConstants.Elements.StringAuthor == reader.LocalName && XmlNodeType.Element == reader.NodeType))
                                    {
                                        throw new XmlException(SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.Annotation));
                                    }

                                    XmlNode node = doc.ReadNode(reader);  // Reads the entire "StringAuthor" tag
                                    if (!reader.IsEmptyElement)
                                    {
                                        _authors.Add(node.InnerText);
                                    }
                                }
                            }
                            reader.Read();    // Reads the "Authors" end tag (or whole tag if it was empty)
                        }
                        else
                        {
                            // The annotation must contain some invalid content which is not part of the schema.
                            throw new XmlException(SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.Annotation));
                        }
                    }
                }

View on GitHub (pinned to 81131a70a4)