{"record":{"id":"8b7dd632847a6b10","repo":"dotnet/wpf","slug":"sr-parsertypeconvertertextneedsendelement","errorCode":null,"errorMessage":"SR.ParserTypeConverterTextNeedsEndElement","messagePattern":"SR\\.ParserTypeConverterTextNeedsEndElement","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReaderHelper.cs","lineNumber":6165,"sourceCode":"                {\n                    // We've just seen the InitializationString, we expect it to be immediately\n                    //  followed by an ElementEnd.\n                    if( xamlNode.TokenType == XamlNodeType.ElementEnd )\n                    {\n                        Debug.Assert(((XamlNode)_xamlNodes[_typeConverterCandidateIndex]).TokenType==XamlNodeType.ElementStart,\n                            \"We've lost track of the ElementStart node, and we're about to die with a cast exception.  See if the ElementStart is still in the ArrayList somewhere, and find out why the pointer got out of sync.\");\n\n                        // We've seen the full <ElementStart>InitializationText</ElementEnd> sequence.\n                        ((XamlElementStartNode)_xamlNodes[_typeConverterCandidateIndex]).CreateUsingTypeConverter = true;\n\n                        // The initializationString would be used as input to the candidate element's TypeConverter.\n                        _typeConverterTextWrittenAndNotProcessed = null;\n                    }\n                    else\n                    {\n                        // Example that would trip this error:\n                        //  <FontFamily>Symbol<Button/></FontFamily>\n                        throw new InvalidOperationException(SR.Format(SR.ParserTypeConverterTextNeedsEndElement, _typeConverterTextWrittenAndNotProcessed));\n                    }\n\n                    // One set of XamlNodes for TypeConverter done, start watching for another.\n                    ResetTypeConverterDecision();\n                }\n                return;\n            }\n\n            // Checking the given XamlNode against the list of types that we know will\n            //  break our ability to use TypeConverter.\n            private bool NodeTypePrecludesTypeConverterUse(XamlNode xamlNode)\n            {\n                XamlNodeType tokenType = xamlNode.TokenType;\n\n                switch(tokenType)\n                {\n                    /////////////////////////////////////////////////////////////\n                    //","sourceCodeStart":6147,"sourceCodeEnd":6183,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReaderHelper.cs#L6147-L6183","documentation":"InvalidOperationException with SR.ParserTypeConverterTextNeedsEndElement thrown when pending TypeConverter text was written but instead of the matching end element the parser encounters a new element start (e.g. a nested element), so the buffered text can never be processed as the element's conversion input.","triggerScenarios":"Parsing XAML like <FontFamily>Symbol<Button/></FontFamily> — text content followed by a child element before the parent's end tag, while the type-converter decision machinery expects the end element to flush the text.","commonSituations":"Accidentally nesting elements inside an element whose content is plain text for a TypeConverter (typo in markup, wrong closing tag, copy/paste of children into a text-only element).","solutions":["Close the text-only element before starting any child element: move the child element outside the parent.","If the parent should contain children, don't rely on TypeConverter text — set the text via a property element or attribute.","Verify closing tags match the correct elements to avoid stray nesting.","Simplify the markup so each element has either text content or element children, never text + children mixed."],"exampleFix":"// before\n<FontFamily>Symbol<Button/></FontFamily>\n// after\n<FontFamily>Symbol</FontFamily>\n<Button/>","handlingStrategy":"validation","validationCode":"// text-only elements must not contain child elements\nbool hasText = el.Nodes().OfType<XText>().Any(n => !string.IsNullOrWhiteSpace(n.Value));\nif (hasText && el.Elements().Any()) throw new InvalidOperationException($\"<{el.Name}> has text content plus child elements\");","typeGuard":null,"tryCatchPattern":"try { return (T)XamlReader.Parse(xaml); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"end\")) { log.Error(ex.Message); throw; }","preventionTips":["Close text-only elements before opening child elements.","Never nest elements inside a TypeConverter-text element.","Lint generated XAML for mixed text/element content."],"tags":["wpf","xaml","typeconverter","parsing"],"backgroundTag":"invalid-state-transition","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"}