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
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
name fills the property Name with the default options:
Program.cs
PropertyNameCaseInsensitive:
Program.cs
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
NotSupportedException the first time it uses the type, for serializing as well as deserializing.
How the constructor is chosen
- The constructor marked
[XmlConstructor]. Marking more than one throws aNotSupportedException. - Otherwise, the public parameterless constructor. A struct is created as its default value.
- Otherwise, the one public constructor that has parameters. If the type has more than one, the serializer throws a
NotSupportedExceptionthat asks you to mark one. - If the type has no public constructor, the serializer creates the object through its parameterless constructor.
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 haveinit 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
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
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 passeddefault. A parameter that has no property or field at all must have a default value. - Don’t set
IgnoreReadOnlyPropertiesfor these types. The option removes get-only properties from the mapping, including the ones a constructor sets. Record properties are unaffected, because they haveinitaccessors. - 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 setPreferredObjectCreationHandling. Immutable collection types such asImmutableArray<T>are created from the items that were read, so they need a setter, aninitaccessor, 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.