dotnet/wpf · error · System.Windows.Markup.XamlParseException

SR.ParserUnexpectedEndEle (unexpected end of element while…

Error message

SR.ParserUnexpectedEndEle (unexpected end of element while parsing XAML)

What it means

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'.

Solutions

  1. Inspect the template XAML for truncated/unclosed elements and regenerate the markup.
  2. If using custom XAML readers/template content loaders, verify the node stream is complete and well-formed.
  3. Update the .NET runtime — since the code marks this path 'should never happen', a framework fix may exist.
  4. Capture the XamlParseException line/position info to locate the malformed template.

Example fix

// before
<ControlTemplate><Grid><Button/></ControlTemplate>  <!-- unclosed Grid -->
// after
<ControlTemplate><Grid><Button/></Grid></ControlTemplate>
Defensive patterns

Strategy: try-catch

Validate before calling

// validate template XAML parses completely before applying
var tmpl = (ControlTemplate)XamlReader.Parse(templateXaml);

Try / catch

try { element.Template = template; }
catch (XamlParseException ex) { log($"{ex.Message} at {ex.BaseUri} line {ex.LineNumber}"); }

Prevention

When it happens

Trigger: Parsing deferred template content whose XAML node stream terminates prematurely inside TrySharingProperty, i.e. a malformed or truncated template node stream reaching ParseNode.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/506e61778fba7a5c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs:1008

                        // The property is NOT shareable.
                        sharedValue = null;
                        return false;
                    }
                    finally
                    {
                        Stack = null;
                    }
                }
            }
            else if (xamlReader.NodeType == System.Xaml.XamlNodeType.GetObject)
            {
                sharedValue = null;
                return false;
            }
            else
            {
                // Should never happen
                throw new System.Windows.Markup.XamlParseException(SR.ParserUnexpectedEndEle);
            }
        }

        private static bool CheckSpecialCasesShareable(Type typeofValue, DependencyProperty property)
        {
            if (typeofValue != typeof(DynamicResourceExtension)
                && typeofValue != typeof(TemplateBindingExtension)
                && typeofValue != typeof(TypeExtension)
                && typeofValue != typeof(StaticExtension))
            {
                // Do not share in the case property type <: IList, Array, IDictionary to maintain compat with v3
                // They wrapped with BamlCollectionHolder in these 3 cases so the value isn't shared.
                if (typeof(IList).IsAssignableFrom(property.PropertyType))
                {
                    return false;
                }
                if (property.PropertyType.IsArray)
                {

View on GitHub (pinned to 81131a70a4)