XmlSerializer treats read-only properties and fields. The attribute and the options mirror [JsonIgnore] and JsonIgnoreCondition in System.Text.Json.
Ignore individual properties
Mark a member with[XmlIgnore]. The serializer never writes it and never reads it.
UserAccount.cs
Program.cs
Password element in incoming XML is treated like any other element that no member maps to: it’s skipped, and the property isn’t set.
View the complete sample on GitHub
Ignore a property when it has no value
Set theCondition property of [XmlIgnore] to one of the XmlIgnoreCondition values.
UserProfile.cs
Program.cs
Bio and SignInCount are gone. Website has no condition, so its null is written as an element marked xsi:nil="true", which is the default for a null member. See Handle null values.
A null collection that is wrapped with [XmlArray] is left out by both conditions, as any other null member is. See Empty and null collections.
The two WhenWriting conditions affect writing only. The member is still read when the XML has it:
Program.cs
Ignore all null or default values
To apply a condition to every member, setDefaultIgnoreCondition on the options. The following example serializes the same object three ways.
Program.cs
PurchaseOrder.cs
- With the default,
Never, every member is written. WhenWritingNulldrops thenullstring. Value types can’t benull, soPlacedandTotalstay.WhenWritingDefaultalso drops the default date and the zero.
[XmlIgnore] attribute on a member overrides the option for that member. For example, [XmlIgnore(Condition = XmlIgnoreCondition.Never)] keeps one member in every document while the option removes the rest.
Setting
DefaultIgnoreCondition to XmlIgnoreCondition.Always throws an ArgumentException, because it would leave out every member. Always only means something on the [XmlIgnore] attribute.Read-only properties
A property with no setter is read-only. It is written by default, which is what you want for a calculated value that the consumer of the XML needs. SetIgnoreReadOnlyProperties to leave such properties out.
Rectangle.cs
Program.cs
Area element in the last document has no effect.
Keep these details in mind:
- A property with a public
initaccessor isn’t read-only: the serializer sets it. A property whosesetorinitaccessor isn’t public is read-only to the serializer, as in System.Text.Json: it is written, and it isn’t set when a document is read. Mark it with[XmlInclude]to have the accessor used. - A read-only collection property, such as
public List<string> Tags { get; } = [];, is written. It is filled when reading only whenPreferredObjectCreationHandling, or[XmlObjectCreationHandling]on the member, isPopulate: the items are then added to the list it holds. Under the default,Replace, it isn’t read.IgnoreReadOnlyPropertiesdoesn’t leave it out, because a collection or a dictionary can be populated without a setter. System.Text.Json makes the same exception. IgnoreReadOnlyPropertiesalso removes a read-only property that a constructor parameter sets. Don’t combine the option with types that are created through a constructor with parameters, unless their properties haveinitaccessors, as record properties do.
Include fields
Fields aren’t serialized by default. SetIncludeFields to serialize public fields, and IgnoreReadOnlyFields to then leave out the ones declared readonly.
Coordinates.cs
Program.cs
Which members are serialized
The serializer considers these members of a type and its base classes:- Public instance properties that have a public getter.
- Public instance fields, when
IncludeFieldsistrue. - Non-public properties, and non-public fields when
IncludeFieldsistrue, that are marked with[XmlInclude].
See also
Handle null values
Choose between
xsi:nil, an empty element, and nothing.Require properties
Make a missing member an error.
XmlIgnoreAttribute
API reference for the attribute.
XmlIgnoreCondition
API reference for the conditions.