unoplatform/uno · error · XamlXmlWriterException
Value type is '{0}' but it must be either string or any type
Error message
Value type is '{0}' but it must be either string or any type that is convertible to string indicated by TypeConverterAttribute. What it means
The internal value-to-string converter in XamlWriterInternalBase throws XamlXmlWriterException when it must serialize a value to text but the value is neither a string nor a type with a ValueSerializer (via the member's ValueSerializer or the type's ValueSerializer). The converter checks for string/empty-string fast paths, then asks the XamlType for a ValueSerializer; if none exists it cannot emit a textual representation and fails.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlWriterInternalBase.cs:252
protected abstract void OnWriteValue (object value);
protected abstract void OnWriteNamespace (NamespaceDeclaration nd);
protected string GetValueString (XamlMember xm, object value)
{
// change XamlXmlReader too if we change here.
if ((value as string) == String.Empty) // FIXME: there could be some escape syntax.
return "\"\"";
if (value is string)
return (string) value;
var xt = value == null ? XamlLanguage.Null : sctx.GetXamlType (value.GetType ());
var vs = xm.ValueSerializer ?? xt.ValueSerializer;
if (vs != null)
return vs.ConverterInstance.ConvertToString (value, service_provider);
else
throw new XamlXmlWriterException (String.Format (CultureInfo.InvariantCulture, "Value type is '{0}' but it must be either string or any type that is convertible to string indicated by TypeConverterAttribute.", value != null ? value.GetType () : null));
}
}
}
View on GitHub (pinned to 0418340488)
Solutions
- Add a TypeConverter (and corresponding ValueSerializer) to the value's type so the writer can convert it to/from string.
- Restructure the object graph so the property holds a string or a type that already has a ValueSerializer (e.g. primitives, DateTime with the XAML serializer).
- If the value is genuinely complex, emit it as a nested object rather than a text value.
- Use a custom XamlXmlWriter subclass overriding value handling if you need bespoke serialization.
Example fix
// before
public class Color { public int R,G,B; } // no converter
// obj.MyColor = new Color {...}; -> save fails
// after
[TypeConverter(typeof(ColorConverter))]
public class Color { public int R,G,B; }
public class ColorConverter : TypeConverter {
public override bool CanConvertTo(ITypeDescriptorContext c, Type t) => t == typeof(string);
public override object ConvertTo(ITypeDescriptorContext c, CultureInfo ci, object v, Type t) => /* format */;
} Defensive patterns
Strategy: validation
Validate before calling
var xt = sctx.GetXamlType(value.GetType());
if (!(value is string) && xt.ValueSerializer == null && member?.ValueSerializer == null)
throw new InvalidOperationException($"Cannot serialize {value.GetType()} as text; add a ValueSerializer/TypeConverter."); Type guard
static bool IsSerializableAsText(object? v, XamlMember? m, XamlSchemaContext sctx) {
if (v is string || v is null) return true;
var xt = sctx.GetXamlType(v.GetType());
return (m?.ValueSerializer ?? xt.ValueSerializer) != null;
} Try / catch
try { XamlServices.Save(writer, instance); }
catch (XamlXmlWriterException ex) when (ex.Message.Contains("convertible to string")) {
// identify the offending value type in the message, then attach a TypeConverter
} Prevention
- Attach [TypeConverter] / ValueSerializer to custom value types you intend to serialize as XAML text.
- Restrict serialized object graphs to types known to have ValueSerializers (primitives, enums, DateTime, etc.).
- Add a round-trip unit test (Save then Load) for each type you persist as XAML.
When it happens
Trigger: Saving an object graph where a property holds a complex type with no associated TypeConverter/ValueSerializer — e.g. a custom class assigned to a content property that the writer must render as text but cannot convert.
Common situations: Serializing domain objects that lack [TypeConverter] or ValueSerializer attributes; persisting object graphs that include non-serializable nested types in text positions; round-tripping XAML where a value type changed to a non-convertible type.
Related errors
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/b4b2930f1cac1ba1.
Report an issue: GitHub.