{"record":{"id":"f6b875cf9fcad0eb","repo":"dotnet/wpf","slug":"sr-requiresxmlnamespacemappinguri","errorCode":null,"errorMessage":"SR.RequiresXmlNamespaceMappingUri","messagePattern":"SR\\.RequiresXmlNamespaceMappingUri","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs","lineNumber":88,"sourceCode":"            ArgumentNullException.ThrowIfNull(text);\n\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n\n#endregion\n\n#region ICollection<XmlNamespaceMapping>\n\n        /// <summary>\n        /// Add XmlNamespaceMapping\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">mapping is null</exception>\n        public void Add(XmlNamespaceMapping mapping)\n        {\n            ArgumentNullException.ThrowIfNull(mapping);\n\n            if (mapping.Uri == null)\n                throw new ArgumentException(SR.RequiresXmlNamespaceMappingUri, nameof(mapping));\n\n            // BUG 983685: change this to take Uri when AddNamespace is fixed to use Uri instead of String.\n            // SECURITY: this workaround (passing the original string) defeats the security benefits of using Uri.\n            this.AddNamespace(mapping.Prefix, mapping.Uri.OriginalString);\n        }\n\n        /// <summary>\n        /// Remove all XmlNamespaceMappings\n        /// </summary>\n        /// <remarks>\n        /// This is potentially an expensive operation.\n        /// It may be cheaper to simply create a new XmlNamespaceMappingCollection.\n        /// </remarks>\n        public void Clear()\n        {\n            int count = Count;\n            XmlNamespaceMapping[] array = new XmlNamespaceMapping[count];\n            CopyTo(array, 0);","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs#L70-L106","documentation":"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.)","triggerScenarios":"Adding an XmlNamespaceMapping whose Uri property was never set (e.g. a mapping constructed and EndInit skipped, or EndInit bypassed entirely in code).","commonSituations":"Programmatically created mappings that skipped EndInit validation; mappings whose Uri was null due to an earlier failed parse; bypassing ISupportInitialize and calling Add directly.","solutions":["Ensure the mapping's EndInit() ran successfully (it enforces non-null Prefix and Uri) before adding.","Use the (string prefix, Uri uri) constructor so Uri is always populated.","Null-check mapping.Uri before calling Add."],"exampleFix":"// before\nvar mapping = new XmlNamespaceMapping();\nmapping.Prefix = \"atom\";\ncollection.Add(mapping); // throws\n// after\nvar mapping = new XmlNamespaceMapping(\"atom\", new Uri(\"http://www.w3.org/2005/Atom\"));\ncollection.Add(mapping);","handlingStrategy":"validation","validationCode":"if (mapping?.Uri == null) throw new ArgumentException(\"Mapping must have a Uri before being added\", nameof(mapping));","typeGuard":"static bool HasUri(XmlNamespaceMapping m) => m?.Uri != null;","tryCatchPattern":"try { collection.Add(mapping); }\ncatch (ArgumentException ex) { log.Error(\"Mapping Uri is null: \" + ex.Message, ex); }","preventionTips":["Ensure EndInit() completed before adding mappings (it validates Prefix and Uri).","Use the (prefix, uri) constructor so Uri is always set.","Null-check mapping.Uri before Add."],"tags":["wpf","xml","null-check","validation"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}