restsharp/RestSharp · error · ArgumentNullException
Parameter value is null
Error message
Parameter value is null
What it means
XmlRestSerializer.Serialize throws ArgumentNullException if the supplied parameter's Value is null. XML serialization of a null object is undefined (System.Xml.Serialization produces no meaningful output), so RestSharp rejects it explicitly rather than emitting an empty document.
Source
Thrown at src/RestSharp/Serializers/Xml/XmlRestSerializer.cs:35
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;
}
public XmlRestSerializer WithXmlDeserializer(IXmlDeserializer xmlDeserializer) {
_deserializer = xmlDeserializer;View on GitHub (pinned to 6a50821692)
Solutions
- Ensure the object passed to XML serialization is non-null before adding it as a body.
- Guard AddXmlBody calls with a null check; skip the body or throw a clearer upstream error.
- If null bodies are legitimately possible, decide on a sentinel/empty object instead of null.
Example fix
// before
request.AddXmlBody(maybeNullObject); // null -> throws on serialize
// after
if (maybeNullObject is not null)
request.AddXmlBody(maybeNullObject); Defensive patterns
Strategy: validation
Validate before calling
if (parameter is XmlParameter xml && xml.Value is null)
throw new ArgumentNullException(nameof(parameter), "XmlParameter value is null");
xmlSerializer.Serialize(parameter); Type guard
static bool HasXmlValue(Parameter p) => p is XmlParameter x && x.Value is not null;
Prevention
- Null-check objects before AddXmlBody.
- For optional bodies, skip the body rather than pass null.
- Unit-test XML serialization paths against nullable reference types.
When it happens
Trigger: Calling XmlRestSerializer.Serialize on an XmlParameter whose Value property is null; using AddXmlBody with a null object that gets wrapped in an XmlParameter with a null value.
Common situations: Passing a null domain object to AddXmlBody; a conditional code path that builds an XmlParameter from a variable that can be null; default(T) for a reference type yielding null.
Related errors
- Request body serialized to null
- Non-string body found with unsupported content type
- Supplied parameter is not an XML parameter
- Class cannot have two properties marked with SerializeAs(Con
- Failed to put file as content because body parameters were a
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/1e4998e107af69b5.
Report an issue: GitHub.