dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterNamespaceAl…

Error message

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterNamespaceAlreadyHasPrefixInCurrentScope, ns))

What it means

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.

Solutions

  1. Write each namespace declaration only once per scope; track namespaces already declared
  2. Reuse the existing prefix instead of assigning a new one
  3. Structure declarations so re-declaration happens in a child scope if a different prefix is required

Example fix

// before
foreach (var ns in allNamespaces) writer.WriteNamespace(new NamespaceDeclaration(ns, PrefixFor(ns)));
// after
foreach (var ns in allNamespaces.Where(ns => !declared.Contains(ns))) { writer.WriteNamespace(new NamespaceDeclaration(ns, PrefixFor(ns))); declared.Add(ns); }
Defensive patterns

Strategy: validation

Validate before calling

if (declaredNamespaces.Contains(nsDecl.Namespace)) return; // already declared in scope
writer.WriteNamespace(nsDecl); declaredNamespaces.Add(nsDecl.Namespace);

Try / catch

try { writer.WriteNamespace(nsDecl); }
catch (XamlXmlWriterException) { /* namespace already has a prefix; reuse it */ }

Prevention

When it happens

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

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

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/46100c7ca06b9dd7. Report an issue: GitHub.

Appendix: source

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

                {
                    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()
            {
                List<KeyValuePair<string, string>> prefixMapList = new List<KeyValuePair<string, string>>();
                foreach (var pair in prefixMap)
                {
                    prefixMapList.Add(pair);
                }

View on GitHub (pinned to 81131a70a4)