dotnet/wpf · error · XmlException
SR.Format(SR.InvalidXmlContent…
Error message
SR.Format(SR.InvalidXmlContent, AnnotationXmlConstants.Elements.Item)
What it means
Item elements must be attribute-only; they may not contain child content. If an Item element is non-empty and the reader lands on anything other than the Item EndElement, ReadXml throws XmlException(SR.InvalidXmlContent, "Item").
Solutions
- Remove all child nodes from Item elements; express all data via Name/Value attributes.
- Regenerate the annotation stream with the standard WPF annotation writer.
- Validate the XML structure before loading.
Example fix
// before <Item Name="k" Value="v"><Extra>x</Extra></Item> // after <Item Name="k" Value="v"/>
Defensive patterns
Strategy: validation
Validate before calling
foreach (var item in doc.Descendants("Item"))
if (!item.IsEmpty && item.Nodes().Any())
throw new InvalidDataException("Item must not contain child content"); Try / catch
try { LoadAnnotations(stream); }
catch (XmlException ex) when (ex.Message.Contains("Item")) { /* strip Item children and retry, or reject the file */ } Prevention
- Treat Item as attribute-only; never nest elements or text inside it.
- Sanitize externally produced annotation XML before loading.
- Document the annotation XML contract for any tool that generates these files.
When it happens
Trigger: Deserializing annotations where an Item has children or inner text, e.g. <Item Name="k" Value="v"><Extra/></Item> or <Item Name="k" Value="v">text</Item>.
Common situations: Tools that serialize key/value pairs as nested elements; hand-edited annotation files; merging markup from other formats into annotation XML.
Related errors
- SR.Format(SR.InvalidAttributeValue…
- SR.Format(SR.InvalidXmlContent…
- SR.Format(SR.InvalidXmlContent…
- SR.Format(SR.InvalidXmlContent…
- SR.Format(SR.InvalidXmlContent, part.PartType.Name)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/252a6daeffaea594.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/LocatorPartList.cs:267
if (value == null)
{
throw new XmlException(SR.Format(SR.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.ItemValue, AnnotationXmlConstants.Elements.Item));
}
reader.MoveToContent();
part.NameValuePairs.Add(name, value);
bool isEmpty = reader.IsEmptyElement;
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 View on GitHub (pinned to 81131a70a4)