dotnet/wpf · error · ArgumentException

SR.RequiresXmlNamespaceMappingUri

Error message

SR.RequiresXmlNamespaceMappingUri

What it means

XmlNamespaceMappingCollection.Add validates that the mapping's Uri is non-null before calling AddNamespace; a mapping without a Uri throws an ArgumentException with SR.RequiresXmlNamespaceMappingUri. (A null Prefix would also produce a broken namespace registration, which is why EndInit guards it upstream.)

Solutions

  1. Ensure the mapping's EndInit() ran successfully (it enforces non-null Prefix and Uri) before adding.
  2. Use the (string prefix, Uri uri) constructor so Uri is always populated.
  3. Null-check mapping.Uri before calling Add.

Example fix

// before
var mapping = new XmlNamespaceMapping();
mapping.Prefix = "atom";
collection.Add(mapping); // throws
// after
var mapping = new XmlNamespaceMapping("atom", new Uri("http://www.w3.org/2005/Atom"));
collection.Add(mapping);
Defensive patterns

Strategy: validation

Validate before calling

if (mapping?.Uri == null) throw new ArgumentException("Mapping must have a Uri before being added", nameof(mapping));

Type guard

static bool HasUri(XmlNamespaceMapping m) => m?.Uri != null;

Try / catch

try { collection.Add(mapping); }
catch (ArgumentException ex) { log.Error("Mapping Uri is null: " + ex.Message, ex); }

Prevention

When it happens

Trigger: Adding an XmlNamespaceMapping whose Uri property was never set (e.g. a mapping constructed and EndInit skipped, or EndInit bypassed entirely in code).

Common situations: Programmatically created mappings that skipped EndInit validation; mappings whose Uri was null due to an earlier failed parse; bypassing ISupportInitialize and calling Add directly.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs:88

            ArgumentNullException.ThrowIfNull(text);

            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
        }

#endregion

#region ICollection<XmlNamespaceMapping>

        /// <summary>
        /// Add XmlNamespaceMapping
        /// </summary>
        /// <exception cref="ArgumentNullException">mapping is null</exception>
        public void Add(XmlNamespaceMapping mapping)
        {
            ArgumentNullException.ThrowIfNull(mapping);

            if (mapping.Uri == null)
                throw new ArgumentException(SR.RequiresXmlNamespaceMappingUri, nameof(mapping));

            // BUG 983685: change this to take Uri when AddNamespace is fixed to use Uri instead of String.
            // SECURITY: this workaround (passing the original string) defeats the security benefits of using Uri.
            this.AddNamespace(mapping.Prefix, mapping.Uri.OriginalString);
        }

        /// <summary>
        /// Remove all XmlNamespaceMappings
        /// </summary>
        /// <remarks>
        /// This is potentially an expensive operation.
        /// It may be cheaper to simply create a new XmlNamespaceMappingCollection.
        /// </remarks>
        public void Clear()
        {
            int count = Count;
            XmlNamespaceMapping[] array = new XmlNamespaceMapping[count];
            CopyTo(array, 0);

View on GitHub (pinned to 81131a70a4)