dotnet/wpf · error

SR.ParserFilterXmlReaderNoDefinitionPrefixChangeAllowed

Error message

SR.ParserFilterXmlReaderNoDefinitionPrefixChangeAllowed

What it means

FilteredXmlReader enforces that the reserved 'x' prefix (XAML language namespace, http://schemas.microsoft.com/winfx/2006/xaml) is not redefined. When it encounters an xmlns declaration that rebinds some prefix other than x:Uid's to the XAML language namespace (or otherwise conflicts with the reserved definition), CheckForNamespaceRedef throws InvalidOperationException.

Solutions

  1. Remove the xmlns declaration that rebinds a non-reserved prefix to the XAML language namespace
  2. Use the reserved 'x:' prefix for XAML language features instead of redefining it
  3. If you need an alias, alias to a different (non-XAML-language) namespace
  4. Validate the XAML's xmlns declarations before passing it through FilteredXmlReader

Example fix

// before
<Page xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml" ...>
// after
<Page xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ...>
Defensive patterns

Strategy: validation

Validate before calling

foreach (var (prefix, ns) in collectedNamespaces) if (ns == "http://schemas.microsoft.com/winfx/2006/xaml" && prefix != "x") throw new XmlException($"Prefix '{prefix}' may not redefine the XAML language namespace");

Type guard

static bool IsLegalNamespaceDecl(string prefix, string ns) => ns != "http://schemas.microsoft.com/winfx/2006/xaml" || prefix == "x";

Try / catch

try { Parse(xamlStream); } catch (InvalidOperationException ex) when (ex.Message.Contains("prefix") || ex.Message.Contains("namespace")) { throw new XamlParseException("Remove xmlns declarations that rebind the XAML language namespace to a non-x prefix"); }

Prevention

When it happens

Trigger: XAML input containing a namespace declaration such as xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml" — any prefix other than the reserved one bound to the XAML language namespace while parsing.

Common situations: Hand-edited or tool-generated XAML that remaps the x namespace; copy-pasted markup with conflicting xmlns aliases; malicious or malformed XAML fed to localization (uid-filtered) parsing.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/FilteredXmlReader.cs:341

        }
    }

    // This is called whenever a node is about to be returned to the user.  We
    //  check to see if it's a "xmlns" node that re-defines the "Definition"
    //  namespace.
    private void CheckForNamespaceRedef()
    {
        // xmlns node example: < [...] xmlns:someprefix="http://schemas.microsoft.com/winfx/2006/xaml" [...}/>
        //  base.Name = "xmlns:someprefix"
        //  base.LocalName = "someprefix"
        //  base.Prefix = "xmlns"
        //  base.Value ="http://schemas.microsoft.com/winfx/2006/xaml" = uidNamespace
        
        if( base.Prefix == "xmlns" &&         // Fixed via XML spec
            base.LocalName != uidPrefix &&    // Hey, this prefix isn't the one we think it is.
            base.Value == uidNamespace)
        {
            throw new InvalidOperationException(
                SR.ParserFilterXmlReaderNoDefinitionPrefixChangeAllowed);
        }
    }

        #endregion Private Methods

        //------------------------------------------------------
        //
        //  Private Fields
        //
        //------------------------------------------------------

        #region Private Fields

        // These are fixed, by definition of the Uid feature.
        private const string uidLocalName = "Uid";
        private const string uidNamespace = XamlReaderHelper.DefinitionNamespaceURI;
        private const string defaultPrefix = "def";

View on GitHub (pinned to 81131a70a4)