{"record":{"id":"506e61778fba7a5c","repo":"dotnet/wpf","slug":"sr-parserunexpectedendele-unexpected-end-of-element-while","errorCode":null,"errorMessage":"SR.ParserUnexpectedEndEle (unexpected end of element while parsing XAML)","messagePattern":"SR\\.ParserUnexpectedEndEle \\(unexpected end of element while parsing XAML\\)","errorType":"exception","errorClass":"System.Windows.Markup.XamlParseException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs","lineNumber":1008,"sourceCode":"                        // The property is NOT shareable.\n                        sharedValue = null;\n                        return false;\n                    }\n                    finally\n                    {\n                        Stack = null;\n                    }\n                }\n            }\n            else if (xamlReader.NodeType == System.Xaml.XamlNodeType.GetObject)\n            {\n                sharedValue = null;\n                return false;\n            }\n            else\n            {\n                // Should never happen\n                throw new System.Windows.Markup.XamlParseException(SR.ParserUnexpectedEndEle);\n            }\n        }\n\n        private static bool CheckSpecialCasesShareable(Type typeofValue, DependencyProperty property)\n        {\n            if (typeofValue != typeof(DynamicResourceExtension)\n                && typeofValue != typeof(TemplateBindingExtension)\n                && typeofValue != typeof(TypeExtension)\n                && typeofValue != typeof(StaticExtension))\n            {\n                // Do not share in the case property type <: IList, Array, IDictionary to maintain compat with v3\n                // They wrapped with BamlCollectionHolder in these 3 cases so the value isn't shared.\n                if (typeof(IList).IsAssignableFrom(property.PropertyType))\n                {\n                    return false;\n                }\n                if (property.PropertyType.IsArray)\n                {","sourceCodeStart":990,"sourceCodeEnd":1026,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs#L990-L1026","documentation":"During deferred template content parsing, TemplateContent.TrySharingProperty decides whether a property value can be shared among template instances. Reaching the final else branch means the node stream ended before an expected element was found — an internal parse-state bug — so the library throws XamlParseException (SR.ParserUnexpectedEndEle) noting it 'should never happen'.","triggerScenarios":"Parsing deferred template content whose XAML node stream terminates prematurely inside TrySharingProperty, i.e. a malformed or truncated template node stream reaching ParseNode.","commonSituations":"Corrupted or hand-crafted BAML/XAML template content, custom XamlReader pipelines producing incomplete node streams, or a WPF framework bug triggered by unusual template markup.","solutions":["Inspect the template XAML for truncated/unclosed elements and regenerate the markup.","If using custom XAML readers/template content loaders, verify the node stream is complete and well-formed.","Update the .NET runtime — since the code marks this path 'should never happen', a framework fix may exist.","Capture the XamlParseException line/position info to locate the malformed template."],"exampleFix":"// before\n<ControlTemplate><Grid><Button/></ControlTemplate>  <!-- unclosed Grid -->\n// after\n<ControlTemplate><Grid><Button/></Grid></ControlTemplate>","handlingStrategy":"try-catch","validationCode":"// validate template XAML parses completely before applying\nvar tmpl = (ControlTemplate)XamlReader.Parse(templateXaml);","typeGuard":null,"tryCatchPattern":"try { element.Template = template; }\ncatch (XamlParseException ex) { log($\"{ex.Message} at {ex.BaseUri} line {ex.LineNumber}\"); }","preventionTips":["Ensure template XAML is well-formed and closed","Avoid hand-editing compiled BAML","Update the .NET runtime if this fires on ordinary markup"],"tags":["wpf","xaml","template","parser","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}