dotnet/wpf · error · XamlUnexpectedParseException
Element ::= . EmptyElement | ( StartElement ElementBody ).
Error message
Element ::= . EmptyElement | ( StartElement ElementBody ).
What it means
XamlPullParser.P_Element implements the grammar rule `Element ::= EmptyElement | (StartElement ElementBody)`. When the current scanner node is neither an EMPTYELEMENT nor an ELEMENT token, it throws XamlUnexpectedParseException with the Element rule text. The pull parser is driven by a scanner; this is a grammar-invariant check that a new element parse actually begins at an element node.
Solutions
- Ensure the XAML source is well-formed XML (validate with an XML parser) before Load.
- Check for stray or duplicated end tags / text nodes where an element should start.
- If using a custom scanner or reader, verify it emits EMPTYELEMENT/ELEMENT nodes where P_Element is invoked.
- Isolate the failing fragment by loading the document progressively and catching XamlUnexpectedParseException to find the offset.
Example fix
<!-- before --> <Grid> </Button> </Grid> <!-- after --> <Grid> <Button /> </Grid>
Defensive patterns
Strategy: try-catch
Validate before calling
// Validate well-formedness before Load var doc = new System.Xml.XmlDocument(); doc.LoadXml(xaml); // throws XmlException if not well-formed
Try / catch
try { return XamlReader.Parse(xaml); } catch (XamlUnexpectedParseException ex) { Log($"Unexpected node {ex.NodeType} where element expected"); return null; } Prevention
- Validate XAML as XML before parsing
- Avoid feeding truncated streams to XamlReader.Load
- Keep custom scanners conformant to ScannerNodeType ordering
When it happens
Trigger: Invoking the parser pipeline (Parse / P_ElementContent / P_PropertyContent paths) on scanner output where the current node is e.g. ENDTAG, TEXT, or a directive when an element start is expected — typically from malformed or non-well-formed XML fed to XamlXmlReader/XamlReader.Load.
Common situations: Feeding truncated XAML streams to XamlReader.Load; custom XamlReader/scanner implementations emitting node sequences out of grammar order; documents with stray closing tags or text where an element is required.
Related errors
- EmptyPropertyElement ::= EMPTYPROPERTYELEMENT.
- NonemptyPropertyElement ::= . PROPERTYELEMENT Content?…
- PropertyElement ::= EmptyPropertyElement |…
- ElementBody ::= ATTRIBUTE* ( PropertyElement | Content )* …
- EmptyElement ::= . EMPTYELEMENT DIRECTIVE* ATTRIBUTE*.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/fca1046158f4f2ed.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/XamlPullParser.cs:114
yield return node;
}
break;
case ScannerNodeType.ELEMENT:
foreach (XamlNode node in P_StartElement())
{
yield return node;
}
foreach (XamlNode node in P_ElementBody())
{
yield return node;
}
break;
default:
throw new XamlUnexpectedParseException(_xamlScanner, nodeType, ElementRuleException);
}
}
///////////////////////////
// EmptyElement ::= EMPTYELEMENT DIRECTIVE* ATTRIBUTE*
//
public IEnumerable<XamlNode> P_EmptyElement()
{
if (_xamlScanner.NodeType != ScannerNodeType.EMPTYELEMENT)
{
throw new XamlUnexpectedParseException(_xamlScanner, _xamlScanner.NodeType,
EmptyElementRuleException);
}
yield return Logic_StartObject(_xamlScanner.Type, _xamlScanner.Namespace);
_xamlScanner.Read();
if (ProvideLineInfo)
{View on GitHub (pinned to 81131a70a4)