{"record":{"id":"7da0bbc74cd21821","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-7da0bb","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteNamespace\"))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteNamespace\"\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":808,"sourceCode":"\n            public virtual void WriteStartMember(XamlXmlWriter writer, XamlMember property)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteStartMember\"));\n            }\n\n            public virtual void WriteEndMember(XamlXmlWriter writer)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndMember\"));\n            }\n\n            public virtual void WriteValue(XamlXmlWriter writer, string value)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteValue\"));\n            }\n\n            public virtual void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteNamespace\"));\n            }\n\n            protected static void WriteMemberAsElement(XamlXmlWriter writer)\n            {\n                Debug.Assert(writer.namespaceScopes.Count > 1);\n\n                Frame frame = writer.namespaceScopes.Peek();\n                Debug.Assert(frame.AllocatingNodeType == XamlNodeType.StartMember);\n\n                XamlType type = frame.Type;\n                XamlMember property = frame.Member;\n\n                string ns;\n\n                XamlType xamlType = property.IsAttachable ? property.DeclaringType : type;\n                string prefix = property.IsAttachable || property.IsDirective ? writer.FindPrefix(property.GetXamlNamespaces(), out ns) : writer.FindPrefix(type.GetXamlNamespaces(), out ns);\n                string local = (property.IsDirective) ? property.Name : $\"{GetTypeName(xamlType)}.{property.Name}\";\n                writer.output.WriteStartElement(prefix, local, ns);","sourceCodeStart":790,"sourceCodeEnd":826,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L790-L826","documentation":"XamlXmlWriter throws XamlXmlWriterException when WriteNamespace is called from a state whose WriterState does not implement namespace writing (the base fallback shown). Namespace declarations can only be written while an object element is being opened, so calling it elsewhere violates the writer's state machine. The error text names the disallowed call to pinpoint the contract break.","triggerScenarios":"Calling XamlXmlWriter.WriteNamespace(NamespaceDeclaration) after the start element has already been written or before any object was started, from a state without a WriteNamespace override.","commonSituations":"Emitting all namespace declarations up-front before WriteStartObject; adding namespaces mid-document in custom XAML generation; porting code from XmlWriter semantics to XamlXmlWriter.","solutions":["Call WriteNamespace immediately after WriteStartObject/WriteGetObject and before members, as the state machine expects","Ensure required namespaces are part of the schema context so they are emitted automatically","Restructure the emission loop to follow the XAML node stream ordering"],"exampleFix":"// before\nwriter.WriteNamespace(ns);\nwriter.WriteStartObject(type);\n// after\nwriter.WriteStartObject(type);\nwriter.WriteNamespace(ns);\nwriter.WriteStartMember(member);","handlingStrategy":"validation","validationCode":"if (!justWroteStartObject) throw new InvalidOperationException(\"WriteNamespace must follow WriteStartObject/WriteGetObject\");","typeGuard":"bool CanWriteNamespace(bool inStartElementOpenPhase) => inStartElementOpenPhase;","tryCatchPattern":"try { writer.WriteNamespace(nsDecl); }\ncatch (XamlXmlWriterException ex) { /* buffer the namespace and emit after next StartObject */ }","preventionTips":["Emit namespaces only in the window right after starting an object","Rely on the schema context to declare namespaces automatically","Never port XmlWriter namespace habits to XamlXmlWriter"],"tags":["xaml","invalid-state-transition","namespaces"],"backgroundTag":"invalid-state-transition","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"}