{"record":{"id":"57b9f6ae1a1e1675","repo":"dotnet/wpf","slug":"sr-mustnotcallsetter-xmlnamespace","errorCode":null,"errorMessage":"SR.MustNotCallSetter","messagePattern":"SR\\.MustNotCallSetter","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XmlNamespace.cs","lineNumber":94,"sourceCode":"            {\n                directive = new UnknownProperty(name,\n                                                declaringType: null, /* xml directives don't have a declaring type. */\n                                                XmlDirectives.Uri);\n            }\n            return directive;\n        }\n\n        public override IEnumerable<XamlProperty> GetAllDirectiveProperties()\n        {\n            throw new NotImplementedException();\n        }\n\n        // --------------- protected ----------------------\n\n        protected override string TargetNamespaceCore\n        {\n            get { return XmlDirectives.Uri; }\n            set { throw new InvalidOperationException(SR.MustNotCallSetter); }\n        }\n\n        // -------------------------------------\n\n        private XamlDirectiveCollection LoadDirectives()\n        {\n            XamlDirectiveCollection directives = null;\n\n            switch (TargetNamespace)\n            {\n            case XmlDirectives.Uri:  // Uri == \"http://www.w3.org/XML/1998/namespace\"\n                directives = new XamlDirectiveCollection(SchemaContext, XmlDirectives.DirectiveInfoTable);\n                break;\n\n            default:\n                directives = new XamlDirectiveCollection(SchemaContext, null);\n                break;\n            }","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XmlNamespace.cs#L76-L112","documentation":"XmlNamespace.TargetNamespaceCore always returns XmlDirectives.Uri and its setter throws InvalidOperationException(SR.MustNotCallSetter). The XML xaml namespace is fixed to the standard XML directives URI, so retargeting it is an invalid operation by design.","triggerScenarios":"Assigning to the TargetNamespace property (backed by TargetNamespaceCore) of an XmlNamespace instance, e.g. schema code that assumes every XamlNamespace has a writable target namespace.","commonSituations":"Generic namespace-manipulation utilities that rename namespaces; code copied from XaslNamespace handling where TargetNamespace is constructor-supplied and may be reassigned.","solutions":["Do not set TargetNamespace on XmlNamespace; it is permanently the XML directives URI.","Create a different XamlNamespace instance if you need a namespace with another target URI.","Skip XmlNamespace instances in code that mutates TargetNamespace (type-check before writing)."],"exampleFix":"// before\nxmlNamespace.TargetNamespace = \"http://my/ns\";\n// after\nvar ns = new XamlNamespace(\"http://my/ns\"); // use a mutable namespace type instead","handlingStrategy":"type-guard","validationCode":"if (ns is XmlNamespace) throw new NotSupportedException(\"XmlNamespace.TargetNamespace is fixed to the XML directives URI.\");","typeGuard":"bool HasMutableTargetNamespace(XamlNamespace ns) => ns is not XmlNamespace;","tryCatchPattern":"try { ns.TargetNamespace = value; } catch (InvalidOperationException) { /* create a new namespace instance instead */ }","preventionTips":["Never retarget XmlNamespace","Create a new XamlNamespace for a different URI","Skip XmlNamespace in generic namespace-renaming code"],"tags":["xaml","invalid-operation","read-only-property"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}