{"record":{"id":"45a800727079586f","repo":"dotnet/wpf","slug":"sr-parsercanonlyhaveonechild","errorCode":null,"errorMessage":"SR.ParserCanOnlyHaveOneChild","messagePattern":"SR\\.ParserCanOnlyHaveOneChild","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReaderHelper.cs","lineNumber":4354,"sourceCode":"                // returns false if there are multiple elements but\n                // the property is not a container.\n                if (!VerifyContentPropertySeesAnElement(ParentContext))\n                {\n                    //We need to error, do work to give a good error message.\n                    string FirstTagName;\n\n                    if (ParentContext.ContextDataType == null)\n                    {\n                        //PropertyElement was the Parent tag.\n                        FirstTagName = $\"{((Type)GrandParentContext.ContextData).Name}.{GrandParentContext.ChildTagLocalName}\";\n                    }\n                    else\n                    {\n                        //ObjectElement was the Parent tag.\n                        FirstTagName = ParentContext.ContextDataType.Name;\n                    }\n\n                    throw new InvalidOperationException(SR.Format(SR.ParserCanOnlyHaveOneChild,\n                        FirstTagName /* Parent or PropertyElement*/,\n                        CurrentContext.ContextDataType.Name /* Child */));\n                }\n            }\n\n            // If we previously synthesised an ElementStart and we now really have\n            // a true element tag as the first child tag, then remove the previously synthesised node from\n            // the reader's node collection, and mark the current context as not\n            // needing to synthesis an end tag.\n            // NOTE: We will only remove the synthesised node if it is a match for\n            //       for the one specified in the actual XAML.  We consider it a match\n            //       for the property class if it is a subclass of the property type\n            if (ParentContext != null &&\n                ParentContext.ContextData != null &&\n                ParentContext.NeedToWriteEndElement &&\n                !ParentContext.FirstChildRead)\n            {\n","sourceCodeStart":4336,"sourceCodeEnd":4372,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReaderHelper.cs#L4336-L4372","documentation":"InvalidOperationException with SR.ParserCanOnlyHaveOneChild raised by XamlReaderHelper when a XAML element or property element that may only contain a single child element receives a second one. The parser stops because the parent's type/property cannot accept more than one child object.","triggerScenarios":"Parsing XAML where two element children appear under a parent (parent tag or property element) whose type is not a collection/content container; raised while processing the second ObjectElement in ReadElementStart flow.","commonSituations":"Declaring two children on a control with a single Content property (e.g. two elements inside <Button> or two elements inside a non-collection property element like <Window.Title>).","solutions":["Remove the extra child element or wrap multiple children in a container (Grid, StackPanel, etc.).","Move one child into a property element (e.g. <Button.Content>, <Button.ToolTip>) if intended.","If the parent should hold many children, change the parent type to a collection type or ensure the target property is a list/dictionary.","Check the parent and child type names in the error message to identify exactly which pair conflicts."],"exampleFix":"// before\n<Button><Grid/><Button.Content>X</Button.Content></Button>\n// after\n<Button><Grid/></Button>","handlingStrategy":"validation","validationCode":"// for a scalar Content property, ensure exactly one child\nint childCount = parentElement.Elements().Count();\nif (childCount > 1) throw new InvalidOperationException($\"{parent.Name} accepts only one child but has {childCount}\");","typeGuard":"static bool AllowsMultipleChildren(Type t) => typeof(System.Collections.IEnumerable).IsAssignableFrom(t) && t != typeof(string);","tryCatchPattern":"try { var obj = (T)XamlReader.Parse(xaml); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"one child\")) { log.Error(ex.Message); throw new XamlContentModelException(ex); }","preventionTips":["Check the content property's type before adding multiple children.","Wrap multiple children in a panel or collection.","Keep one child per property element."],"tags":["wpf","xaml","parsing","content-model"],"backgroundTag":"invalid-argument-value","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"}