{"record":{"id":"d37f56886928bca6","repo":"dotnet/wpf","slug":"sr-objectreaderxamlnamedelementalreadyregistered","errorCode":null,"errorMessage":"SR.ObjectReaderXamlNamedElementAlreadyRegistered","messagePattern":"SR\\.ObjectReaderXamlNamedElementAlreadyRegistered","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":1781,"sourceCode":"                });\n\n                // some collection or dictionary's object node might have been removed\n                // i.e. XamlNode is now changed to GetObject.  since we are now naming\n                // the object, we need to add the object node back\n                if (XamlNode.NodeType == XamlNodeType.GetObject)\n                {\n                    Debug.Assert(Object is not null);\n                    var xamlType = context.LocalAssemblyAwareGetXamlType(Object.GetType());\n                    XamlNode = new XamlNode(XamlNodeType.StartObject, xamlType);\n                }\n            }\n\n            public override void EnsureNoDuplicateNames(Stack<HashSet<string>> namesInCurrentScope)\n            {\n                if (!string.IsNullOrEmpty(Name) &&\n                    !namesInCurrentScope.Peek().Add(Name))\n                {\n                    throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderXamlNamedElementAlreadyRegistered, Name));\n                }\n\n                // Recurse through all of the values I have.\n                foreach (var property in Properties)\n                {\n                    var propertyInfo = (MemberMarkupInfo)property;\n                    foreach (var ov in propertyInfo.Children)\n                    {\n                        ((ObjectOrValueMarkupInfo)ov).EnsureNoDuplicateNames(namesInCurrentScope);\n                    }\n                }\n            }\n\n            private static string ConvertTypeAndMethodToString(Type type, string methodName, SerializerContext context)\n            {\n                string typestring = context.ConvertXamlTypeToString(context.LocalAssemblyAwareGetXamlType(type));\n\n                return $\"{typestring}.{methodName}\";","sourceCodeStart":1763,"sourceCodeEnd":1799,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L1763-L1799","documentation":"XamlObjectReader throws this when two elements in the same XAML namescope carry the same x:Name (or Name). XAML names must be unique within a namescope, so duplicate names make the round-tripped markup ambiguous and namescope resolution impossible.","triggerScenarios":"new XamlObjectReader(obj) on a graph (e.g. WPF element tree or object tree with Name properties set) where EnsureNoDuplicateNames finds the same Name string twice within one namescope.","commonSituations":"Programmatic UI construction that assigns the same Name in a loop; cloning/copying element trees without renaming; deserializing trees that were merged from two documents with colliding names.","solutions":["Assign unique names within each namescope before constructing the reader","Clear the Name property on duplicates if the name is not needed","Put conflicting subtrees into separate XamlNamescope-equivalent containers","Generate names programmatically (e.g. with a counter) when cloning trees"],"exampleFix":"// before (in a clone loop)\nclone.Name = original.Name;\n// after\nclone.Name = original.Name + \"_\" + index;","handlingStrategy":"validation","validationCode":"var seen = new HashSet<string>(StringComparer.Ordinal);\nvoid Walk(DependencyObject o) { var n = o is FrameworkElement fe ? fe.Name : null; if (!string.IsNullOrEmpty(n) && !seen.Add(n)) throw new InvalidOperationException($\"Duplicate name: {n}\"); foreach (object c in LogicalTreeHelper.GetChildren(o).OfType<DependencyObject>()) Walk(c); }","typeGuard":null,"tryCatchPattern":"try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains(\"AlreadyRegistered\")) { /* deduplicate names */ }","preventionTips":["Auto-generate names (suffix with counters/GUIDs) when cloning trees","Validate name uniqueness before constructing the reader","Keep namescopes separate for merged subtrees"],"tags":["xaml","namescope","naming"],"backgroundTag":"duplicate-name-conflict","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"}