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 deserializer when two or more properties on the target class are decorated with [DeserializeAs(Content = true)]. Content=true marks a property as the element's inner text content; XML allows only one text node per element, so two such annotations are ambiguous and rejected.

Source

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

            if (!typeIsPublic || !prop.CanWrite)
                continue;

            var    deserializeFromContent   = false;
            var    isNameDefinedInAttribute = false;
            XName? name                     = null;
            var    attributes               = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false);

            if (attributes.Any()) {
                var attribute = (DeserializeAsAttribute)attributes.First();

                name                     = attribute.Name.AsNamespaced(Namespace);
                isNameDefinedInAttribute = !string.IsNullOrEmpty(name?.LocalName);

                deserializeFromContent = attribute.Content;

                if (deserializeFromContentAttributeAlreadyUsed && deserializeFromContent)
                    throw new ArgumentException("Class cannot have two properties marked with SerializeAs(Content = true) attribute.");

                deserializeFromContentAttributeAlreadyUsed |= deserializeFromContent;
            }

            if (name == null) name = prop.Name.AsNamespaced(Namespace);

            var value = GetValueFromXml(root, name, prop, isNameDefinedInAttribute);

            if (value == null) {
                // special case for text content node
                if (deserializeFromContent) {
                    var textNode = root!.Nodes().FirstOrDefault(n => n is XText);

                    if (textNode != null) {
                        value = ((XText)textNode).Value;
                        prop.SetValue(x, value, null);
                    }

View on GitHub (pinned to 6a50821692)

Solutions

  1. Inspect the target class and remove [DeserializeAs(Content = true)] from all but one property.
  2. Decide which single property represents the element's text content and keep only that attribute.
  3. Use a unit test asserting at most one Content=true property per class to prevent regression.

Example fix

// before
[DeserializeAs(Content = true)] public string Value { get; set; }
[DeserializeAs(Content = true)] public string Extra { get; set; }

// after
[DeserializeAs(Content = true)] public string Value { get; set; }
public string Extra { get; set; }
Defensive patterns

Strategy: validation

Validate before calling

var contentProps = typeof(T).GetProperties().Select(p => p.GetCustomAttributes(typeof(DeserializeAsAttribute), false).Cast<DeserializeAsAttribute>().FirstOrDefault(a => a?.Content == true)).Count(a => a != null);
if (contentProps > 1) throw new InvalidOperationException("Multiple [DeserializeAs(Content=true)] properties detected");

Try / catch

try { var result = client.GetAsync<T>(req); } catch (ArgumentException ex) when (ex.Message.Contains("two properties marked with SerializeAs(Content = true)")) { /* fix the DTO, only one Content property allowed */ }

Prevention

When it happens

Trigger: A class used as an XML deserialization target has two properties each annotated with [DeserializeAs(Content = true)]. Triggered during ExecuteAsync<T> with the XML deserializer when mapping the root element to the type.

Common situations: Copy-paste of an attribute across properties; misunderstanding of what Content=true means (intending 'include this in serialization'); refactoring that moved the attribute without removing the old one.

Related errors


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