dotnet/wpf · error · XamlSchemaException

SR.BadXmlnsPrefix

Error message

SR.BadXmlnsPrefix

What it means

System.Xaml throws XamlSchemaException(SR.BadXmlnsPrefix) while loading [assembly: XmlnsPrefix] attributes when the xmlns or prefix string is null or empty. Both values are required to declare a preferred prefix for a namespace.

Solutions

  1. Provide non-empty xmlns and prefix strings in the XmlnsPrefix attribute.
  2. Remove the attribute if no preferred prefix is intended.
  3. Validate attribute constants are populated before shipping.

Example fix

// before
[assembly: System.Windows.Markup.XmlnsPrefix("clr-namespace:Ns", "")]
// after
[assembly: System.Windows.Markup.XmlnsPrefix("clr-namespace:Ns", "controls")]
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(xmlns) || string.IsNullOrEmpty(prefix)) throw new InvalidOperationException("XmlnsPrefix requires non-empty xmlns and prefix");

Try / catch

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

Prevention

When it happens

Trigger: An assembly declares [assembly: XmlnsPrefix("", "p")] or [assembly: XmlnsPrefix("clr-namespace:Ns", "")]; LoadPrefixes -> LoadPrefixesHelper sees an empty value and throws.

Common situations: Hand-edited AssemblyInfo attributes, designer tooling emitting empty prefix values, refactors that cleared one argument.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

            }
            else
            {
                Attribute[] attributes = Attribute.GetCustomAttributes(assembly, typeof(XmlnsPrefixAttribute));
                foreach (Attribute attr in attributes)
                {
                    XmlnsPrefixAttribute xmlnsPrefixAttr = (XmlnsPrefixAttribute)attr;
                    LoadPrefixesHelper(result, xmlnsPrefixAttr.XmlNamespace, xmlnsPrefixAttr.Prefix, assembly);
                }
            }

            return result;
        }

        private void LoadPrefixesHelper(Dictionary<string, string> result, string xmlns, string prefix, Assembly assembly)
        {
            if (string.IsNullOrEmpty(prefix) || string.IsNullOrEmpty(xmlns))
            {
                throw new XamlSchemaException(SR.Format(SR.BadXmlnsPrefix, assembly.FullName));
            }

            string oldPrefix;
            if (!result.TryGetValue(xmlns, out oldPrefix) ||
                GetPreferredPrefix(oldPrefix, prefix) == prefix)
            {
                result[xmlns] = prefix;
            }
        }

        private string LoadRootNamespace()
        {
            Assembly assembly = Assembly;
            if (assembly is null)
            {
                return null;
            }

View on GitHub (pinned to 81131a70a4)