restsharp/RestSharp · error · FormatException

Couldn't parse the value of

Error message

Couldn't parse the value of '{value}' into the '{prop.Name}' property, because it isn't a type of '{prop.PropertyType}'.

What it means

Thrown during XML deserialization when a primitive property value from the XML cannot be converted (via ChangeType) to the property's declared type, producing a FormatException. The inner FormatException's original message is replaced with this contextual one naming the value and the target property/type.

Solutions

  1. Inspect the raw XML response content to see the offending value for the named property.
  2. Change the property type to string or a nullable type so conversion does not fail, then parse manually.
  3. Ensure the server returns values compatible with the property's declared primitive type.

Example fix

// before
public int Count { get; set; }

// after
public int? Count { get; set; }
Defensive patterns

Strategy: try-catch

Try / catch

try { var result = client.GetAsync<T>(req); } catch (DeserializationException ex) when (ex.InnerException is FormatException) { /* log raw XML, relax property types, or parse manually */ }

Prevention

When it happens

Trigger: The XML element/attribute text cannot be parsed as the property's primitive type, e.g. an element mapped to an int contains 'abc', or a value mapped to a numeric type holds a non-numeric string, raising FormatException inside ChangeType.

Common situations: Schema drift where the server sends strings in numeric fields; locale/number-format mismatches; empty string in a non-nullable numeric field; boolean fields receiving non-boolean text other than those XmlConvert.ToBoolean handles.

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/1c02d2fea8f2a97f. Report an issue: GitHub.

Appendix: source

Thrown at src/RestSharp.Serializers.Xml/XmlDeserializer.cs:185

                    continue;
                }

                type = type.GetGenericArguments()[0].GetTypeInfo();
            }

            var asType = type.AsType();

            if (asType == typeof(bool)) {
                var toConvert = value.ToString()!.ToLower(Culture);

                prop.SetValue(x, XmlConvert.ToBoolean(toConvert), null);
            }
            else if (type.IsPrimitive) {
                try {
                    prop.SetValue(x, value.ChangeType(asType), null);
                }
                catch (FormatException ex) {
                    throw new FormatException(
                        $"Couldn't parse the value of '{value}' into the '{prop.Name}'" +
                        $" property, because it isn't a type of '{prop.PropertyType}'.",
                        ex.InnerException
                    );
                }
            }
            else if (type.IsEnum) {
                var converted = type.AsType().FindEnumValue(value.ToString(), Culture);

                prop.SetValue(x, converted, null);
            }
            else if (asType == typeof(Uri)) {
                var uri = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);

                prop.SetValue(x, uri, null);
            }
            else if (asType == typeof(string)) {
                prop.SetValue(x, value, null);

View on GitHub (pinned to 6a50821692)