restsharp/RestSharp · error · ArgumentException
Supplied parameter is not an XML parameter
Error message
Supplied parameter is not an XML parameter
What it means
XmlRestSerializer.Serialize throws ArgumentException if the Parameter passed to it is not an XmlParameter. The XML serializer path requires an XmlParameter because it carries the optional XmlNamespace used during serialization; a generic Parameter does not. This guards the serializer against being called with the wrong parameter subtype.
Source
Thrown at src/RestSharp/Serializers/Xml/XmlRestSerializer.cs:32
namespace RestSharp.Serializers.Xml;
public class XmlRestSerializer(IXmlSerializer serializer, IXmlDeserializer deserializer) : IRestSerializer {
IXmlDeserializer _deserializer = deserializer;
IXmlSerializer _serializer = serializer;
public XmlRestSerializer() : this(new DotNetXmlSerializer(), new DotNetXmlDeserializer()) { }
public ISerializer Serializer => _serializer;
public IDeserializer Deserializer => _deserializer;
public string[] AcceptedContentTypes => ContentType.XmlAccept;
public SupportsContentType SupportsContentType => contentType => contentType.Value.EndsWith("xml", StringComparison.InvariantCultureIgnoreCase);
public DataFormat DataFormat => DataFormat.Xml;
public string? Serialize(Parameter parameter) {
if (parameter is not XmlParameter xmlParameter)
throw new ArgumentException("Supplied parameter is not an XML parameter", nameof(parameter));
if (parameter.Value == null)
throw new ArgumentNullException(nameof(parameter), "Parameter value is null");
var savedNamespace = _serializer.Namespace;
_serializer.Namespace = xmlParameter.XmlNamespace ?? savedNamespace;
var result = _serializer.Serialize(parameter.Value);
_serializer.Namespace = savedNamespace;
return result;
}
public XmlRestSerializer WithXmlSerializer(IXmlSerializer xmlSerializer) {
_serializer = xmlSerializer;
return this;
}View on GitHub (pinned to 6a50821692)
Solutions
- When serializing XML bodies, add them as XmlParameter (or use AddXmlBody, which creates the correct parameter type).
- Do not pass a BodyParameter/JsonParameter into XmlRestSerializer.Serialize.
- Ensure the request's DataFormat and the body parameter type are consistent (Xml DataFormat -> XmlParameter).
Example fix
// before
var param = new BodyParameter("", obj, ContentType.Xml); // wrong type
xmlSerializer.Serialize(param); // throws
// after
var param = new XmlParameter(obj, "", xmlNamespace: null);
xmlSerializer.Serialize(param); Defensive patterns
Strategy: type-guard
Validate before calling
if (parameter is not XmlParameter)
throw new ArgumentException($"Expected XmlParameter, got {parameter.GetType().Name}", nameof(parameter));
xmlSerializer.Serialize(parameter); Type guard
static bool IsXmlParameter(Parameter p) => p is XmlParameter;
Prevention
- Use AddXmlBody which constructs the correct XmlParameter internally.
- Never pass BodyParameter/JsonParameter to XmlRestSerializer.Serialize.
- Keep DataFormat and parameter subtype consistent per request.
When it happens
Trigger: Calling the XML serializer's Serialize(Parameter) with a plain BodyParameter, JsonParameter, or any non-XmlParameter; triggering XML serialization indirectly by adding a non-XML body parameter on a request whose DataFormat.Xml serializer is selected.
Common situations: Manually invoking the serializer with a wrong parameter type; mixing AddBody/AddJsonBody parameters into an XML request; custom code that constructs parameters without using XmlParameter for XML bodies.
Related errors
- Parameter value is null
- Class cannot have two properties marked with SerializeAs(Con
- Request body serialized to null
- Non-string body found with unsupported content type
- Failed to put file as content because POST parameters were a
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/5414cdadc045c632.
Report an issue: GitHub.