Skip to main content
An immutable type can’t be changed after it’s created, so a serializer can’t create it empty and then set its properties. XmlSerializer handles this the way System.Text.Json does: it reads the XML first, then calls a constructor with the values it found. This article covers records, classes with a constructor that takes parameters, [XmlConstructor], init-only properties, and structs. Serializing an immutable type needs nothing special. Everything here is about deserializing.

Records

A positional record works with no attributes and no options. The serializer matches each constructor parameter to the property of the same name.
Temperature.cs
Program.cs
The code produces the following output:
Elements may appear in any order. The serializer holds the values until it reaches the end tag, then calls the constructor. A record can also have ordinary properties besides its positional ones. Those are set after the constructor runs. View the complete sample on GitHub

Classes with a parameterized constructor

A class with read-only properties and a constructor works the same way. C# constructor parameters are usually camelCase, and the serializer matches a parameter to the member with the same name without regard to case, as System.Text.Json does.
Measurement.cs
The parameter name fills the property Name with the default options:
Program.cs
The code produces the following output:
The case of a parameter never matters. The case of an element or attribute name in the document does, unless you set PropertyNameCaseInsensitive:
Program.cs
The code produces the following output:
If two members differ only by case, each takes the parameter that is spelled exactly like it.

Choose a constructor with XmlConstructor

When a type has more than one constructor, mark the one the serializer should use with [XmlConstructor]. The constructor doesn’t have to be public.
DateRange.cs
Program.cs
The code produces the following output:
Each parameter of the marked constructor must match a serialized member, by the member’s C# name or by its XML name and without regard to case, unless the parameter has a default value. If one doesn’t, the serializer throws a NotSupportedException the first time it uses the type, for serializing as well as deserializing.

How the constructor is chosen

  1. The constructor marked [XmlConstructor]. Marking more than one throws a NotSupportedException.
  2. Otherwise, the public parameterless constructor. A struct is created as its default value.
  3. Otherwise, the one public constructor that has parameters. If the type has more than one, the serializer throws a NotSupportedException that asks you to mark one.
  4. If the type has no public constructor, the serializer creates the object through its parameterless constructor.
Parameters with no default value are required: XML that lacks one is rejected with an XmlException. See Constructor parameters.
These are the rules of System.Text.Json. Rule 2 means that a public parameterless constructor wins over a constructor with parameters, so the members of such a type need setters to be read. If a type has both and its members are get-only, put [XmlConstructor] on the constructor with parameters. A struct you define always needs the attribute for its constructor to be used.

Init-only properties

A type whose properties have init accessors needs no constructor parameters. The serializer creates the object and sets each init property as it reads the element, as it does for a set accessor.
ConnectionSettings.cs
Program.cs
The code produces the following output:
A property that isn’t in the XML keeps its initializer, so Port is 5432 in the second object. To make a member mandatory instead, add the required modifier. See Require properties.

Structs

Structs and record structs are supported. Unless a constructor is marked with [XmlConstructor], a struct is created as its default value and its settable and init members are set, which is how the positional readonly record struct that follows is read. A struct whose properties are get-only needs [XmlConstructor] on the constructor that sets them.
GeoPoint.cs
Program.cs
The code produces the following output:

What to watch for

  • A constructor parameter whose property is ignored gets its default value. If you exclude a positional record property with [property: XmlIgnore], the record is still read through its constructor, and that parameter is passed default. A parameter that has no property or field at all must have a default value.
  • Don’t set IgnoreReadOnlyProperties for these types. The option removes get-only properties from the mapping, including the ones a constructor sets. Record properties are unaffected, because they have init accessors.
  • A collection property with no setter isn’t read by default. A property such as public List<string> Tags { get; } = []; is written. To have the items added to the list it holds when it is read, mark it [XmlObjectCreationHandling(XmlObjectCreationHandling.Populate)] or set PreferredObjectCreationHandling. Immutable collection types such as ImmutableArray<T> are created from the items that were read, so they need a setter, an init accessor, or a constructor parameter. See Serialize collections.
  • Trimming and Native AOT. The examples here find and call constructors through reflection. For a trimmed or Native AOT application, see Use source generation.

See also

Require properties

Make members and constructor parameters mandatory.

Deserialize

Read XML into objects from each kind of source.

Configure options

Set PropertyNameCaseInsensitive and the other options once.

XmlConstructorAttribute

API reference for the attribute.