dotnet/wpf · error · InvalidOperationException

SR.NullParentNode

Error message

SR.NullParentNode

What it means

AddChildToCurrentParent throws this InvalidOperationException when there is no current parent node on the deserializer's stack at the time a child node is added. This reflects an internal structural invariant of the BAML tree build being violated — the tree is malformed or the deserializer state is inconsistent.

Solutions

  1. Rebuild/redeploy the resource assembly to fix malformed BAML
  2. Validate the BAML by opening the original XAML and recompiling
  3. Avoid custom tools that emit BAML directly; stick to the XAML compiler
  4. If reproducible with valid BAML, report as a WPF deserializer bug

Example fix

// before
// BAML with child node outside any element
// after
// recompile XAML: ensure all nodes are inside a root element
Defensive patterns

Strategy: try-catch

Try / catch

try { tree = deserializer.LoadBaml(); } catch (InvalidOperationException ex) when (ex.Message.Contains("parent")) { throw new ResourceLoadException("Malformed BAML tree structure", ex); }

Prevention

When it happens

Trigger: A BAML node that adds a child arrives while the parent stack is empty — malformed BAML with unbalanced structure, or a bug/state corruption during LoadBamlImp processing.

Common situations: Corrupt or hand-crafted BAML with unbalanced element nesting; custom BAML writers emitting children without enclosing elements.

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/fb4832c553463937. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Globalization/BamlResourceDeserializer.cs:315

            return new BamlTree(_root, _nodeCount);
            //notice that we don't close the input stream because we don't own it.
        }

        private void PushNodeToStack(BamlTreeNode node)
        {
            _currentParent?.AddChild(node);

            _bamlTreeStack.Push(node);
            _currentParent = node;
            _nodeCount++;
        }

        private void AddChildToCurrentParent(BamlTreeNode node)
        {
            if (_currentParent == null)
            {
                throw new InvalidOperationException(SR.NullParentNode);
            }

            _currentParent.AddChild(node);
            _nodeCount++;
        }

        private void PopStack()
        {
            BamlTreeNode node = _bamlTreeStack.Pop();
            if (node.Children != null)
            {
                // pop properties from property inheritance stack as well
                foreach (BamlTreeNode child in node.Children)
                {
                    if (child is BamlStartComplexPropertyNode propertyNode)
                    {
                        PopPropertyFromStack(propertyNode.PropertyName);
                    }

View on GitHub (pinned to 81131a70a4)