restsharp/RestSharp · error · ArgumentException
Class cannot have two properties marked with SerializeAs(Con
Error message
Class cannot have two properties marked with SerializeAs(Content = true) attribute.
What it means
Thrown by the XML serializer (Serialize path) when two or more properties on the class are marked [SerializeAs(Content = true)]. Content=true designates one property as the element's text content; XML permits a single text node, so a second one is rejected during serialization.
Source
Thrown at src/RestSharp.Serializers.Xml/XmlSerializer.cs:144
var propType = prop.PropertyType;
var useAttribute = false;
var setTextContent = false;
var options = prop.GetAttribute<SerializeAsAttribute>();
if (options != null) {
name = options.Name.IsNotEmpty()
? options.Name
: name;
name = options.TransformName(name!);
useAttribute = options.Attribute;
setTextContent = options.Content;
if (textContentAttributeAlreadyUsed && setTextContent)
throw new ArgumentException("Class cannot have two properties marked with SerializeAs(Content = true) attribute.");
textContentAttributeAlreadyUsed |= setTextContent;
}
else if (globalOptions != null) {
name = globalOptions.TransformName(name);
}
var nsName = name.AsNamespaced(Namespace);
var element = new XElement(nsName!);
if (propType.GetTypeInfo().IsPrimitive ||
propType.GetTypeInfo().IsValueType ||
propType == typeof(string)) {
var value = GetSerializedValue(rawValue);
if (useAttribute) {
root.Add(new XAttribute(name, value));
continue;View on GitHub (pinned to 6a50821692)
Solutions
- Remove [SerializeAs(Content = true)] from all but one property on the class.
- Keep a single property as the text-content representative of the element.
- Add a unit test that serializes the type to catch duplicate Content attributes early.
Example fix
// before
[SerializeAs(Content = true)] public string Text { get; set; }
[SerializeAs(Content = true)] public string Note { get; set; }
// after
[SerializeAs(Content = true)] public string Text { get; set; }
public string Note { get; set; } Defensive patterns
Strategy: validation
Validate before calling
var count = typeof(T).GetProperties().Select(p => p.GetCustomAttributes(typeof(SerializeAsAttribute), false).Cast<SerializeAsAttribute>().FirstOrDefault(a => a?.Content == true)).Count(a => a != null);
if (count > 1) throw new InvalidOperationException("Multiple [SerializeAs(Content=true)] properties detected"); Try / catch
try { request.AddXmlBody(obj); } catch (ArgumentException ex) when (ex.Message.Contains("two properties marked with SerializeAs(Content = true)")) { /* remove duplicate Content attribute on the DTO */ } Prevention
- Keep exactly one [SerializeAs(Content = true)] per XML serializable DTO.
- Unit-test XML serialization of each DTO to catch duplicate attributes.
When it happens
Trigger: Serializing an object to XML (e.g. AddXmlBody or request body serialization) where the class has two properties decorated with [SerializeAs(Content = true)].
Common situations: Decorating multiple string properties as content intentionally (misunderstanding); copy-paste error; refactoring that duplicated the attribute.
Related errors
- Class cannot have two properties marked with SerializeAs(Con
- Supplied parameter is not an XML parameter
- Parameter value is null
- Couldn't parse the value of '{value}' into the '{prop.Name}'
- Request body serialized to null
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/cb06807886ca2ff7.
Report an issue: GitHub.