{"record":{"id":"46100c7ca06b9dd7","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-46100c","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterNamespaceAlreadyHasPrefixInCurrentScope, ns))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterNamespaceAlreadyHasPrefixInCurrentScope, ns\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":749,"sourceCode":"                {\n                    ns = XamlLanguage.Xml1998Namespace;\n                    return true;\n                }\n\n                return prefixMap.TryGetValue(prefix, out ns);\n            }\n\n            public void AssignNamespacePrefix(string ns, string prefix)\n            {\n                if (prefixMap.ContainsKey(prefix))\n                {\n                    // we don't allow re-defining the same prefix-to-namespace mapping twice\n                    throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterPrefixAlreadyDefinedInCurrentScope, prefix));\n                }\n\n                if (namespaceMap.ContainsKey(ns))\n                {\n                    throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterNamespaceAlreadyHasPrefixInCurrentScope, ns));\n                }\n\n                prefixMap[prefix] = ns;\n                namespaceMap[ns] = prefix;\n            }\n\n            public bool IsEmpty()\n            {\n                return (namespaceMap.Count == 0);\n            }\n\n            public List<KeyValuePair<string, string>> GetSortedPrefixMap()\n            {\n                List<KeyValuePair<string, string>> prefixMapList = new List<KeyValuePair<string, string>>();\n                foreach (var pair in prefixMap)\n                {\n                    prefixMapList.Add(pair);\n                }","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L731-L767","documentation":"In a given scope, each namespace may have only one assigned prefix. When AssignNamespacePrefix finds the namespace already mapped to a prefix, it throws XamlXmlWriterException rather than silently re-mapping it, which would invalidate references already written with the old prefix.","triggerScenarios":"Calling WriteNamespace (or otherwise triggering AssignNamespacePrefix) for a namespace URI that already has a prefix in the current scope, even if the new prefix differs from the old one.","commonSituations":"Loops that unconditionally write namespaces for every type encountered, re-writing the same namespace; merging node streams that each declare the same namespace; code that re-assigns a namespace to a shorter prefix mid-document.","solutions":["Write each namespace declaration only once per scope; track namespaces already declared","Reuse the existing prefix instead of assigning a new one","Structure declarations so re-declaration happens in a child scope if a different prefix is required"],"exampleFix":"// before\nforeach (var ns in allNamespaces) writer.WriteNamespace(new NamespaceDeclaration(ns, PrefixFor(ns)));\n// after\nforeach (var ns in allNamespaces.Where(ns => !declared.Contains(ns))) { writer.WriteNamespace(new NamespaceDeclaration(ns, PrefixFor(ns))); declared.Add(ns); }","handlingStrategy":"validation","validationCode":"if (declaredNamespaces.Contains(nsDecl.Namespace)) return; // already declared in scope\nwriter.WriteNamespace(nsDecl); declaredNamespaces.Add(nsDecl.Namespace);","typeGuard":null,"tryCatchPattern":"try { writer.WriteNamespace(nsDecl); }\ncatch (XamlXmlWriterException) { /* namespace already has a prefix; reuse it */ }","preventionTips":["Track namespaces declared per scope; write each once","Reuse existing prefixes rather than reassigning","Filter duplicate declarations when merging node streams"],"tags":["xaml","namespace-collision","prefix"],"backgroundTag":"conflicting-config-options","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"}