Skip to main content
By default, 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# required modifier.
  • The [XmlRequired] attribute, for when you don’t want the compiler to enforce the member in object initializers.
Registration.cs
The following example deserializes three documents: one that is complete, one without Email, and one without Plan.
Program.cs
The code produces the following output:
A missing required member throws an 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
The code produces the following output:
“Required” means the member must be in the XML with a value. It doesn’t validate the value. To reject an empty string, check it in your own code after deserializing. For how 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
The code produces the following output:
  • PercentOff has a default value, so the first document may leave it out and the constructor receives 10.
  • Code has no default value, so the second document is rejected.
  • With RespectRequiredConstructorParameters set to false, a missing parameter receives the default value of its type. Here that puts null in a property declared as a non-nullable string.
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. Set UnmappedMemberHandling 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 code produces the following output:
Keep these details in mind:
  • 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 (xmlns and xmlns: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. With Disallow, an element for it is an error.
  • When the type has a member marked with [XmlExtensionData], unmapped elements and attributes are collected there instead, and Disallow doesn’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.