{"record":{"id":"6e4ac9b29fab29c4","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-xamlxmlwriter","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterPrefixAlreadyDefinedInCurrentScope, prefix))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterPrefixAlreadyDefinedInCurrentScope, prefix\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":744,"sourceCode":"            }\n\n            public bool TryLookupNamespace(string prefix, out string ns)\n            {\n                if (prefix == \"xml\")\n                {\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            {","sourceCodeStart":726,"sourceCodeEnd":762,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L726-L762","documentation":"When the writer assigns a prefix for a namespace (AssignNamespacePrefix on the current scope), it first checks that the prefix is not already bound in the current scope. Re-defining an existing prefix-to-namespace mapping is not allowed, and the writer throws XamlXmlWriterException.","triggerScenarios":"Two different namespaces in the same scope request the same prefix (e.g. two namespaces both defaulting to prefix \"p\"), or WriteNamespace is called twice with the same prefix but different URIs without leaving scope.","commonSituations":"Writing multiple assemblies' namespaces that share a default prefix; generated code assigning prefixes from a fixed pool without collision checks; writing a document that repeats an xmlns declaration with a changed URI.","solutions":["Generate unique prefixes (e.g. p, p1, p2...) when a prefix is already taken in scope","Check the writer's existing prefix mapping (via FindPrefix behavior) before assigning","Scope namespace declarations so re-declarations occur in nested object scopes, not the same scope"],"exampleFix":"// before\nwriter.WriteNamespace(new NamespaceDeclaration(ns1, \"p\"));\nwriter.WriteNamespace(new NamespaceDeclaration(ns2, \"p\")); // collision\n// after\nwriter.WriteNamespace(new NamespaceDeclaration(ns1, \"p\"));\nwriter.WriteNamespace(new NamespaceDeclaration(ns2, \"p1\"));","handlingStrategy":"validation","validationCode":"// generate collision-free prefixes\nstring prefix = basePrefix; int i = 1;\nwhile (assignedPrefixes.Contains(prefix)) prefix = basePrefix + i++;","typeGuard":null,"tryCatchPattern":"try { writer.WriteNamespace(nsDecl); }\ncatch (XamlXmlWriterException) { /* retry with a unique prefix */ }","preventionTips":["Use a prefix allocator that checks existing assignments","Avoid fixed default prefixes when writing many namespaces","Prefer letting the writer pick prefixes (FindPrefix) over forcing them"],"tags":["xaml","prefix-collision","namespace"],"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"}