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

  1. Ensure the object passed to XML serialization is non-null before adding it as a body.
  2. Guard AddXmlBody calls with a null check; skip the body or throw a clearer upstream error.
  3. 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

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


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