null. XML has three, and they mean different things. This article shows which one XmlSerializer writes for a null member, how to leave it out with XmlIgnoreCondition, and what each shape becomes when you deserialize.
xsi:nil is an attribute defined by XML Schema. The xsi prefix is bound to http://www.w3.org/2001/XMLSchema-instance, which the library exposes as XmlNamespaces.Xsi.
Write null as xsi:nil
By default, the serializer writes anull member as an empty element marked xsi:nil="true". A reader can then tell null apart from an empty string, and the document round-trips exactly.
Program.cs
Contact.cs
xsi prefix itself. The serializer doesn’t move the declaration to the document element.
Only an element can be nil. A null member that is mapped to an attribute or to the element’s text is left out, whatever the options say.
View the complete sample on GitHub
Leave null members out
SetXmlSerializerOptions.DefaultIgnoreCondition to XmlIgnoreCondition.WhenWritingNull to write nothing for a null member. This gives the smallest document. It is the same option, with the same meaning, as JsonSerializerOptions.DefaultIgnoreCondition set to JsonIgnoreCondition.WhenWritingNull.
Program.cs
Nickname and Age are never assigned. They are null here because that is their initial value. If the type’s constructor or a property initializer assigns something else, the member keeps that value.
To leave out one member instead of all of them, use [XmlIgnore(Condition = XmlIgnoreCondition.WhenWritingNull)]. See Ignore properties.
XmlIgnoreCondition.WhenWritingNullReference leaves out null references only. A nullable value type with no value, such as an int?, is still written with xsi:nil="true". That is how System.Xml.Serialization.XmlSerializer treats the two, and it is the condition XmlSerializerOptions.Bcl uses.
A
null collection that has an [XmlArray] wrapper is a null member like any other. WhenWritingNull leaves the wrapper out, and by default the wrapper is written with xsi:nil="true", which reads back as null. An empty collection is different: it is written as an empty wrapper and reads back as an empty collection. The ignore condition applies to members only. A null item of a collection and a null value of a dictionary are always written, marked xsi:nil="true", as System.Text.Json always writes them as null. See Collections.Read the three shapes
Reading doesn’t depend on the ignore condition. Whatever the options are, the serializer reads each shape the same way.Program.cs
Describe is a helper in the sample that prints null, empty string, or the quoted text.
The code produces the following output:
xsi:nilaccepts the valuestrueand1. Withfalseor0, the element is read normally.- An empty element is the empty string. That is a valid
string, and it is anXmlExceptionfor a type whose text can’t be empty, such as a number. xsi:nil="true"on a member that can’t benull, such as anint, is anXmlException:The element is nil but type 'System.Int32' is not nillable.- A member marked
[XmlRequired]or declared with therequiredkeyword can’t be nil. See Required properties.
Respect nullable annotations
By default the serializer doesn’t look at nullable annotations: a member declaredstring accepts xsi:nil and can be written as xsi:nil. Set XmlSerializerOptions.RespectNullableAnnotations to true to refuse a null where the annotation says there is none. It is the same option, with the same rules, as JsonSerializerOptions.RespectNullableAnnotations.
- Reading: an element marked
xsi:nil="true"for a member, or for the constructor parameter the member fills, that doesn’t acceptnull. - Writing: a
nullheld by a member annotated as never returningnull, whether the member is an element, an XML attribute, or text. Anullthat the ignore condition leaves out isn’t written, so it isn’t refused.
[AllowNull] and [DisallowNull] change what a member accepts. [MaybeNull] and [NotNull] change what it returns.
The serializer doesn’t check the following:
- A member the document leaves out. Use required properties for that.
- The items of a collection and the values of a dictionary.
- The document root.
- A member compiled without nullable annotations.
- A member whose type is a type parameter with no
class,struct, ornotnullconstraint.
[XmlSourceGenerationOptions(RespectNullableAnnotations = true)] sets the option on the options of Default.
Nullable value types
ANullable<T> member, such as int?, follows the same rules as a reference type. When it has a value, the element is written exactly as for T. When it is null, it is written as xsi:nil, or left out when the ignore condition says so. The first example on this page shows an int? written as xsi:nil and read back as null.
A value type that isn’t nullable is always written, because it always has a value. To leave out a value type that holds its default value, use XmlIgnoreCondition.WhenWritingDefault. See Ignore properties.
Serialize a null root
When the value you pass toSerialize is itself null, the serializer writes the document element of the declared type, marked nil. Deserializing that document returns null. System.Text.Json writes the literal null in the same situation.
Program.cs
null for a null root whatever DefaultIgnoreCondition is.
View the complete sample on GitHub
Choose a setting
See also
Ignore properties
Leave out individual members that are null or have a default value.
Required properties
Reject a document that omits a member or marks it nil.
Configure options
Create and reuse
XmlSerializerOptions.XmlIgnoreCondition
API reference for the enumeration.