dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterPrefixAlrea…

Error message

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterPrefixAlreadyDefinedInCurrentScope, prefix))

What it means

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.

Solutions

  1. Generate unique prefixes (e.g. p, p1, p2...) when a prefix is already taken in scope
  2. Check the writer's existing prefix mapping (via FindPrefix behavior) before assigning
  3. Scope namespace declarations so re-declarations occur in nested object scopes, not the same scope

Example fix

// before
writer.WriteNamespace(new NamespaceDeclaration(ns1, "p"));
writer.WriteNamespace(new NamespaceDeclaration(ns2, "p")); // collision
// after
writer.WriteNamespace(new NamespaceDeclaration(ns1, "p"));
writer.WriteNamespace(new NamespaceDeclaration(ns2, "p1"));
Defensive patterns

Strategy: validation

Validate before calling

// generate collision-free prefixes
string prefix = basePrefix; int i = 1;
while (assignedPrefixes.Contains(prefix)) prefix = basePrefix + i++;

Try / catch

try { writer.WriteNamespace(nsDecl); }
catch (XamlXmlWriterException) { /* retry with a unique prefix */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6e4ac9b29fab29c4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:744

            }

            public bool TryLookupNamespace(string prefix, out string ns)
            {
                if (prefix == "xml")
                {
                    ns = XamlLanguage.Xml1998Namespace;
                    return true;
                }

                return prefixMap.TryGetValue(prefix, out ns);
            }

            public void AssignNamespacePrefix(string ns, string prefix)
            {
                if (prefixMap.ContainsKey(prefix))
                {
                    // we don't allow re-defining the same prefix-to-namespace mapping twice
                    throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterPrefixAlreadyDefinedInCurrentScope, prefix));
                }

                if (namespaceMap.ContainsKey(ns))
                {
                    throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterNamespaceAlreadyHasPrefixInCurrentScope, ns));
                }

                prefixMap[prefix] = ns;
                namespaceMap[ns] = prefix;
            }

            public bool IsEmpty()
            {
                return (namespaceMap.Count == 0);
            }

            public List<KeyValuePair<string, string>> GetSortedPrefixMap()
            {

View on GitHub (pinned to 81131a70a4)