{"record":{"id":"ccad21d2474ea99e","repo":"dotnet/wpf","slug":"sr-parsermultiroot","errorCode":null,"errorMessage":"SR.ParserMultiRoot","messagePattern":"SR\\.ParserMultiRoot","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/TreeBuilder.cs","lineNumber":206,"sourceCode":"        ///  ParserHooks implementation\n        /// </summary>\n        internal ParserHooks ParserHooks\n        {\n            get { return _hooks; }\n            set { _hooks = value; }\n        }\n\n        /// <summary>\n        /// Root element\n        /// </summary>\n        internal object GetRoot()\n        {\n            ArrayList roots = RecordReader.RootList;                \n            object root = (null == roots || 0 == roots.Count) ? null : roots[0];\n\n            if (root != null && roots.Count > 1)\n            {\n                throw new XamlParseException(SR.ParserMultiRoot);\n            }\n            \n            return root; \n        }\n\n        /// <summary>\n        /// BamlRecordReader used for Loading the Tree\n        /// </summary>\n        internal BamlRecordReader RecordReader\n        {\n            get { return _bamlRecordReader; }\n            set { _bamlRecordReader = value; }\n        }\n\n#endif\n\n#if !PBTCOMPILER\n        /// <summary>","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/TreeBuilder.cs#L188-L224","documentation":"TreeBuilder.GetRoot builds an object tree from the XAML record reader's root list and requires exactly one root element. When the parsed XAML contains more than one root node, it throws XamlParseException (ParserMultiRoot) because a loose XAML document must have a single root object.","triggerScenarios":"Loading XAML content with multiple top-level elements (e.g. two sibling elements at document level, or a comment/root mix that yields 2+ records) through XamlReader.Load / TreeBuilder.Build.","commonSituations":"Copy-pasting several controls into a XAML file without a wrapping container; programmatic XAML generation emitting multiple roots; concatenating XAML fragments.","solutions":["Wrap multiple top-level elements in a single container (Grid, StackPanel, etc.)","Trim the XAML so exactly one root element remains","If loading fragments, load each fragment separately"],"exampleFix":"// before\n<Button/>\n<Label/>\n// after\n<StackPanel>\n  <Button/>\n  <Label/>\n</StackPanel>","handlingStrategy":"validation","validationCode":"// before load: ensure a single root\nvar rootElems = doc.Root.ElementsAfterSelf().ToList();\nif (doc.Root.ElementsAfterSelf().Any())\n    throw new InvalidOperationException(\"XAML must contain exactly one root element\");","typeGuard":null,"tryCatchPattern":"try { var obj = XamlReader.Parse(xaml); }\ncatch (XamlParseException ex) when (ex.Message.Contains(\"root\")) { /* wrap content in a single container */ }","preventionTips":["Guarantee exactly one top-level element per XAML document","Wrap fragment collections in a container element","When generating XAML programmatically, emit one root node"],"tags":["wpf","xaml","parse-error","structure"],"backgroundTag":"xml-parse-error","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"}