{"record":{"id":"a7f4b5c3a4cf8732","repo":"dotnet/wpf","slug":"argumentexception-sr-xamlxmlwritercannotwritenonstringvalue","errorCode":null,"errorMessage":"ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue, nameof(value))","messagePattern":"ArgumentException\\(SR\\.XamlXmlWriterCannotWriteNonstringValue, nameof\\(value\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":215,"sourceCode":"        public override void WriteEndMember()\n        {\n            CheckIsDisposed();\n            currentState.WriteEndMember(this);\n        }\n\n        public override void WriteValue(object value)\n        {\n            CheckIsDisposed();\n            if (value is null)\n            {\n                WriteStartObject(XamlLanguage.Null);\n                WriteEndObject();\n            }\n            else\n            {\n                if (value is not string s)\n                {\n                    throw new ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue, nameof(value));\n                }\n\n                currentState.WriteValue(this, s);\n            }\n        }\n\n        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)\n        {\n            CheckIsDisposed();\n\n            ArgumentNullException.ThrowIfNull(namespaceDeclaration);\n\n            if (namespaceDeclaration.Prefix is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull, nameof(namespaceDeclaration));\n            }\n\n            if (namespaceDeclaration.Namespace is null)","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L197-L233","documentation":"XamlXmlWriter.WriteValue only accepts string values (XAML nodes are text-based); passing any non-string value throws ArgumentException with the message XamlXmlWriterCannotWriteNonstringValue. Non-string primitives must be converted with a type converter/value serializer before being written as node values.","triggerScenarios":"Calling WriteValue(42), WriteValue(someObject), or WriteValue(null as object) instead of WriteValue(\"42\"); piping raw IXamlReader values whose underlying objects are not strings.","commonSituations":"Custom node-stream generation that forgets to stringify values; copying nodes from an object-graph reader (XamlObjectReader) whose values are CLR objects into an XML writer without a value-conversion step; migrating code from writers that accepted object values.","solutions":["Convert the value to string first, using its type converter or value serializer (e.g. schemaContext.GetValueConverter / TypeConverter.ConvertToString).","Call WriteValue only with string instances; write other data as StartObject/StartMember nodes instead.","Use XamlServices or a transform (e.g. XamlXmlWriter fed via NodesToXml-style pipeline) that performs value conversion automatically."],"exampleFix":"// before\nwriter.WriteValue(42); // ArgumentException\n// after\nwriter.WriteValue(TypeDescriptor.GetConverter(42).ConvertToString(42)); // \"42\"","handlingStrategy":"type-guard","validationCode":"if (value is not string s)\n    throw new ArgumentException(\"WriteValue requires a string; convert via TypeConverter first\", nameof(value));","typeGuard":"bool IsWritableValue(object v) => v is string;","tryCatchPattern":"try { writer.WriteValue(value); }\ncatch (ArgumentException) { /* convert value via its TypeConverter/ValueSerializer, or write as object node */ }","preventionTips":["Convert all primitive/object values to strings via TypeConverter.ConvertToString before WriteValue.","When copying nodes from XamlObjectReader, run a value-conversion transform.","Represent non-string data as StartObject/StartMember nodes instead of values."],"tags":["xaml","type-mismatch","value"],"backgroundTag":"type-mismatch","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"}