Skip to main content
Not every member of a type belongs in the XML. This article shows how to exclude a member completely, how to leave it out only when it has no value, and how 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
The code produces the following output:
The second half matters as much as the first. A 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 the Condition property of [XmlIgnore] to one of the XmlIgnoreCondition values.
UserProfile.cs
Program.cs
The code produces the following output:
In the first document, 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
The code produces the following output:

Ignore all null or default values

To apply a condition to every member, set DefaultIgnoreCondition on the options. The following example serializes the same object three ways.
Program.cs
PurchaseOrder.cs
The code produces the following output:
  • With the default, Never, every member is written.
  • WhenWritingNull drops the null string. Value types can’t be null, so Placed and Total stay.
  • WhenWritingDefault also drops the default date and the zero.
An [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. Set IgnoreReadOnlyProperties to leave such properties out.
Rectangle.cs
Program.cs
The code produces the following output:
When reading, a read-only property can’t be set, so the Area element in the last document has no effect. Keep these details in mind:
  • A property with a public init accessor isn’t read-only: the serializer sets it. A property whose set or init accessor 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 when PreferredObjectCreationHandling, or [XmlObjectCreationHandling] on the member, is Populate: the items are then added to the list it holds. Under the default, Replace, it isn’t read. IgnoreReadOnlyProperties doesn’t leave it out, because a collection or a dictionary can be populated without a setter. System.Text.Json makes the same exception.
  • IgnoreReadOnlyProperties also 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 have init accessors, as record properties do.

Include fields

Fields aren’t serialized by default. Set IncludeFields to serialize public fields, and IgnoreReadOnlyFields to then leave out the ones declared readonly.
Coordinates.cs
Program.cs
The code produces the following output:
Within one type, properties are written before fields.

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 IncludeFields is true.
  • Non-public properties, and non-public fields when IncludeFields is true, that are marked with [XmlInclude].
Static members, indexers, and properties with no getter are never serialized.

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.