dotnet/wpf · error · XmlException

SR.Format(SR.InvalidXmlContent, part.PartType.Name)

Error message

SR.Format(SR.InvalidXmlContent, part.PartType.Name)

What it means

Inside a ContentLocatorPart, only <Item> children are permitted. If the reader encounters any other child element (or stray content), ReadXml throws XmlException(SR.InvalidXmlContent, part.PartType.Name), naming the offending locator part's type.

Solutions

  1. Ensure ContentLocatorPart elements contain only Item children.
  2. Align the producing tool's schema with the WPF annotation format, or strip unknown children before loading.
  3. Regenerate the annotation store from the original application.

Example fix

// before
<MyPart xmlns="..."><OtherThing/></MyPart>
// after
<MyPart xmlns="..."><Item Name="k" Value="v"/></MyPart>
Defensive patterns

Strategy: validation

Validate before calling

foreach (var part in doc.Descendants().Where(e => e.Name.Namespace == annotationNs && e.Name.LocalName != "Item" && e.Name.LocalName != "ContentLocator"))
    if (part.Elements().Any(c => c.Name.LocalName != "Item"))
        throw new InvalidDataException($"Locator part {part.Name.LocalName} has non-Item children");

Try / catch

try { LoadAnnotations(stream); }
catch (XmlException ex) when (ex.Message.Contains("InvalidXmlContent")) { /* drop unknown locator-part children and re-import */ }

Prevention

When it happens

Trigger: Deserializing a ContentLocatorPart element whose children include elements other than Item, e.g. <MyPart xmlns="..."><OtherThing/></MyPart>.

Common situations: Newer or third-party annotation producers adding extension elements; hand-edited files; version drift where a producer wrote richer parts than this reader understands.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/LocatorPartList.cs:278

                                reader.Read();  // Read the beginning of the complete Item tag

                                if (!isEmpty)
                                {
                                    if (!(XmlNodeType.EndElement == reader.NodeType && AnnotationXmlConstants.Elements.Item == reader.LocalName))
                                    {
                                        // Should not contain any content, only attributes
                                        throw new XmlException(SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.Item));
                                    }
                                    else
                                    {
                                        reader.Read(); // Read the end of the Item tag
                                    }
                                }
                            }
                            else
                            {
                                // The locator part contains data other than just "Item" tags
                                throw new XmlException(SR.Format(SR.InvalidXmlContent, part.PartType.Name));
                            }
                        }
                    }

                    _parts.Add(part);

                    reader.Read();  // Read the ContentLocatorPart element                
                }
            }

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

        #endregion IXmlSerializable Implementation

        #endregion Public Methods

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

View on GitHub (pinned to 81131a70a4)