dotnet/wpf · error · XamlSchemaException

SR.DuplicateXmlnsCompat

Error message

SR.DuplicateXmlnsCompat

What it means

System.Xaml throws XamlSchemaException(SR.DuplicateXmlnsCompat) when two XmlnsCompat attributes in the same assembly map the same old namespace. LoadOldToNewNs builds a dictionary keyed by old namespace; a second registration for an existing key is ambiguous and rejected.

Solutions

  1. Remove or unify the duplicated XmlnsCompat attributes so each old namespace is mapped exactly once.
  2. Search AssemblyInfo files for repeated XmlnsCompat entries with the same first argument.
  3. If both mappings are needed, choose one canonical old-namespace spelling.

Example fix

// before (two attributes)
[assembly: XmlnsCompat("clr-namespace:OldNs", "clr-namespace:NewNsA")]
[assembly: XmlnsCompat("clr-namespace:OldNs", "clr-namespace:NewNsB")]
// after (one)
[assembly: XmlnsCompat("clr-namespace:OldNs", "clr-namespace:NewNsA")]
Defensive patterns

Strategy: validation

Validate before calling

var dupes = compatPairs.GroupBy(p => p.OldNs).Where(g => g.Count() > 1).ToList(); if (dupes.Any()) throw new InvalidOperationException($"Duplicate XmlnsCompat old ns: {dupes[0].Key}");

Try / catch

try { loadXaml(); } catch (XamlSchemaException ex) when (ex.Message.Contains("DuplicateXmlnsCompat")) { /* dedupe attributes */ }

Prevention

When it happens

Trigger: An assembly declares two [assembly: XmlnsCompat] attributes with identical oldns values; the second one hits result.ContainsKey(oldns) in LoadOldToNewNsHelper.

Common situations: Merging assemblies or copy-pasting compat attributes during a namespace migration, duplicated attributes in AssemblyInfo.cs across partial class/attribute files.

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/96191859f6c77347. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/XmlNsInfo.cs:349

                    // Read in the attribute value
                    XmlnsCompatibleWithAttribute xmlnsCompatAttr = (XmlnsCompatibleWithAttribute)attr;
                    LoadOldToNewNsHelper(result, xmlnsCompatAttr.OldNamespace, xmlnsCompatAttr.NewNamespace, assembly);
                }
            }

            return result;
        }

        private void LoadOldToNewNsHelper(Dictionary<string, string> result, string oldns, string newns, Assembly assembly)
        {
            if (string.IsNullOrEmpty(newns) || string.IsNullOrEmpty(oldns))
            {
                throw new XamlSchemaException(SR.Format(SR.BadXmlnsCompat, assembly.FullName));
            }

            if (result.ContainsKey(oldns))
            {
                throw new XamlSchemaException(SR.Format(SR.DuplicateXmlnsCompat, assembly.FullName, oldns));
            }

            result.Add(oldns, newns);
        }

        private Dictionary<string, string> LoadPrefixes()
        {
            Dictionary<string, string> result = new Dictionary<string, string>(StringComparer.Ordinal);

            Assembly assembly = Assembly;
            if (assembly is null)
            {
                return result;
            }

            if (assembly.ReflectionOnly)
            {
                EnsureReflectionOnlyAttributeData();

View on GitHub (pinned to 81131a70a4)