dotnet/wpf · error · ArgumentException

ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue…

Error message

ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue, nameof(value))

What it means

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.

Solutions

  1. Convert the value to string first, using its type converter or value serializer (e.g. schemaContext.GetValueConverter / TypeConverter.ConvertToString).
  2. Call WriteValue only with string instances; write other data as StartObject/StartMember nodes instead.
  3. Use XamlServices or a transform (e.g. XamlXmlWriter fed via NodesToXml-style pipeline) that performs value conversion automatically.

Example fix

// before
writer.WriteValue(42); // ArgumentException
// after
writer.WriteValue(TypeDescriptor.GetConverter(42).ConvertToString(42)); // "42"
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not string s)
    throw new ArgumentException("WriteValue requires a string; convert via TypeConverter first", nameof(value));

Type guard

bool IsWritableValue(object v) => v is string;

Try / catch

try { writer.WriteValue(value); }
catch (ArgumentException) { /* convert value via its TypeConverter/ValueSerializer, or write as object node */ }

Prevention

When it happens

Trigger: Calling WriteValue(42), WriteValue(someObject), or WriteValue(null as object) instead of WriteValue("42"); piping raw IXamlReader values whose underlying objects are not strings.

Common situations: 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.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a7f4b5c3a4cf8732. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:215

        public override void WriteEndMember()
        {
            CheckIsDisposed();
            currentState.WriteEndMember(this);
        }

        public override void WriteValue(object value)
        {
            CheckIsDisposed();
            if (value is null)
            {
                WriteStartObject(XamlLanguage.Null);
                WriteEndObject();
            }
            else
            {
                if (value is not string s)
                {
                    throw new ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue, nameof(value));
                }

                currentState.WriteValue(this, s);
            }
        }

        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)
        {
            CheckIsDisposed();

            ArgumentNullException.ThrowIfNull(namespaceDeclaration);

            if (namespaceDeclaration.Prefix is null)
            {
                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull, nameof(namespaceDeclaration));
            }

            if (namespaceDeclaration.Namespace is null)

View on GitHub (pinned to 81131a70a4)