XmlSerializer accepts XML that doesn’t match the target type exactly. A member with no element keeps its default value, and an element with no member is skipped. This article shows how to tighten both: how to mark members as required, and how to reject content the type doesn’t map. Requirements apply to deserialization only. They don’t change what is written.
Mark members as required
There are two ways to require a member, the same two that System.Text.Json offers:- The C#
requiredmodifier. - The
[XmlRequired]attribute, for when you don’t want the compiler to enforce the member in object initializers.
Registration.cs
Email, and one without Plan.
Program.cs
XmlException. The message names the member by its XML name, so a member renamed with [XmlPropertyName("email")] is reported as email. Referrer isn’t required, so the first document is accepted without it.
A required member can be an element, an XML attribute, or element text. A required collection that is written as repeated elements counts as present when the XML has at least one item.
View the complete sample on GitHub
Present, nil, and empty
XML can say three different things about a value that isn’t there. A required member treats them differently:- The element is missing: an error, as shown above.
- The element is present and marked
xsi:nil="true": an error. A required member can’t be nil. - The element is present and empty: accepted. For a
string, the value is the empty string.
Program.cs
null is written and read in general, see Handle null values.
Constructor parameters
When a type is created through a constructor with parameters, as a positional record is, every parameter that has no default value is required.Coupon.cs
Program.cs
PercentOffhas a default value, so the first document may leave it out and the constructor receives10.Codehas no default value, so the second document is rejected.- With
RespectRequiredConstructorParametersset tofalse, a missing parameter receives the default value of its type. Here that putsnullin a property declared as a non-nullablestring.
RespectRequiredConstructorParameters defaults to true. In System.Text.Json the option of the same name defaults to false. If you port types that rely on missing constructor arguments being filled with defaults, set the option to false or give the parameters default values.Reject unmapped members
An unmapped member is an element or attribute in the XML that no member of the type maps to. SetUnmappedMemberHandling to XmlUnmappedMemberHandling.Disallow to make it an error. The following example reads two documents, one with an unknown element and one with an unknown attribute, under both settings.
Program.cs
- The setting is on the options, so it applies to every type in the object graph. There is no attribute that sets it for one type.
- Namespace declarations (
xmlnsandxmlns:prefix),xsi:nil,xsi:type, and the attributes the serializer writes for polymorphism and preserved references are never unmapped members. - A member marked
[XmlIgnore]isn’t mapped. WithDisallow, an element for it is an error. - When the type has a member marked with
[XmlExtensionData], unmapped elements and attributes are collected there instead, andDisallowdoesn’t reject them.
XmlSerializerOptions.Strict turns on Disallow together with three other checks: duplicate elements, comments, and nullable annotations. See Configure XmlSerializerOptions.
See also
Deserialize
Read XML into objects, and see the default handling of missing and unknown elements.
Immutable types
Deserialize records and types with constructor parameters.
Handle null values
Tell missing, nil, and empty apart.
XmlRequiredAttribute
API reference for the attribute.