dotnet/wpf · error · ArgumentException
SR.RequiresXmlNamespaceMapping (formatted with value type…
Error message
SR.RequiresXmlNamespaceMapping (formatted with value type name)
What it means
XmlNamespaceMappingCollection.AddChild (the IAddChild content path used by XAML) only accepts XmlNamespaceMapping instances. If the supplied object is of any other type, it throws an ArgumentException with SR.RequiresXmlNamespaceMapping formatted with the value's actual type name.
Solutions
- Only place XmlNamespaceMapping elements inside the collection.
- Call collection.Add(new XmlNamespaceMapping(prefix, uri)) in code instead of AddChild with raw values.
- Check the element type in XAML; a typo like <Prefix> as a direct child is not supported.
Example fix
// before
xmlDataProvider.XmlNamespaceMapper... // AddChild("atom"); // throws
// after
var mapping = new XmlNamespaceMapping("atom", new Uri("http://www.w3.org/2005/Atom"));
collection.Add(mapping); Defensive patterns
Strategy: type-guard
Validate before calling
if (child is not XmlNamespaceMapping) throw new ArgumentException("Only XmlNamespaceMapping children are supported", nameof(child)); Type guard
static bool IsNamespaceMapping(object value) => value is XmlNamespaceMapping;
Try / catch
try { collection.AddChild(value); }
catch (ArgumentException ex) { log.Error("XmlNamespaceMappingCollection requires XmlNamespaceMapping children", ex); } Prevention
- Only place <XmlNamespaceMapping> elements as collection children in XAML.
- Call the typed Add(XmlNamespaceMapping) overload instead of AddChild in code.
- Validate element types in XAML when building collections dynamically.
When it happens
Trigger: Adding a child to an XmlNamespaceMappingCollection in XAML or via AddChild that is not an <XmlNamespaceMapping> element, e.g. a plain string or wrong element type.
Common situations: XAML content-element typo (nesting the wrong element inside XmlNamespaceMapping); code calling AddChild directly with a string prefix instead of a mapping object.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.ChildHasWrongType (formatted with type name…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/dbb83e0eac6c2fc8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs:50
/// <summary>
/// IAddChild implementation
/// <see cref="IAddChild"/>
/// </summary>
/// <param name="value"></param>
void IAddChild.AddChild(object value)
{
AddChild(value);
}
/// <summary>
/// IAddChild implementation
/// </summary>
/// <param name="value"></param>
protected virtual void AddChild(object value)
{
XmlNamespaceMapping mapping = value as XmlNamespaceMapping;
if (mapping == null)
throw new ArgumentException(SR.Format(SR.RequiresXmlNamespaceMapping, value.GetType().FullName), nameof(value));
Add(mapping);
}
/// <summary>
/// IAddChild implementation
/// </summary>
/// <param name="text"></param>
void IAddChild.AddText(string text)
{
AddText(text);
}
/// <summary>
/// IAddChild implementation
/// </summary>
/// <param name="text"></param>
protected virtual void AddText(string text)View on GitHub (pinned to 81131a70a4)