{"record":{"id":"fca1046158f4f2ed","repo":"dotnet/wpf","slug":"element-emptyelement-startelement-elementbody","errorCode":null,"errorMessage":"Element ::= . EmptyElement | ( StartElement ElementBody ).","messagePattern":"Element ::= \\. EmptyElement \\| \\( StartElement ElementBody \\)\\.","errorType":"exception","errorClass":"XamlUnexpectedParseException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/XamlPullParser.cs","lineNumber":114,"sourceCode":"                    yield return node;\n                }\n\n                break;\n\n            case ScannerNodeType.ELEMENT:\n                foreach (XamlNode node in P_StartElement())\n                {\n                    yield return node;\n                }\n\n                foreach (XamlNode node in P_ElementBody())\n                {\n                    yield return node;\n                }\n\n                break;\n            default:\n                throw new XamlUnexpectedParseException(_xamlScanner, nodeType, ElementRuleException);\n            }\n        }\n\n        ///////////////////////////\n        //  EmptyElement ::= EMPTYELEMENT DIRECTIVE* ATTRIBUTE*\n        //\n        public IEnumerable<XamlNode> P_EmptyElement()\n        {\n            if (_xamlScanner.NodeType != ScannerNodeType.EMPTYELEMENT)\n            {\n                throw new XamlUnexpectedParseException(_xamlScanner, _xamlScanner.NodeType,\n                    EmptyElementRuleException);\n            }\n\n            yield return Logic_StartObject(_xamlScanner.Type, _xamlScanner.Namespace);\n            _xamlScanner.Read();\n            if (ProvideLineInfo)\n            {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/XamlPullParser.cs#L96-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"<!-- before -->\n<Grid>\n  </Button>\n</Grid>\n\n<!-- after -->\n<Grid>\n  <Button />\n</Grid>","handlingStrategy":"try-catch","validationCode":"// Validate well-formedness before Load\nvar doc = new System.Xml.XmlDocument();\ndoc.LoadXml(xaml); // throws XmlException if not well-formed","typeGuard":null,"tryCatchPattern":"try { return XamlReader.Parse(xaml); } catch (XamlUnexpectedParseException ex) { Log($\"Unexpected node {ex.NodeType} where element expected\"); return null; }","preventionTips":["Validate XAML as XML before parsing","Avoid feeding truncated streams to XamlReader.Load","Keep custom scanners conformant to ScannerNodeType ordering"],"tags":["xaml","parser","grammar","wpf"],"backgroundTag":"xml-parse-error","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}