Skip to main content
XmlSerializerOptions holds every setting that changes how XmlSerializer writes and reads XML. It is the counterpart of JsonSerializerOptions in System.Text.Json and follows the same rules: create an instance once, reuse it, and don’t change it after its first use. This article covers those rules and lists every option with its default.

Reuse options instances

An options instance is more than a bag of settings. The first time you serialize or deserialize a type with it, the serializer works out how the type maps to XML and stores the result on the instance. Every later call with the same instance reuses that work. A new instance starts again from nothing. Create the options once, keep them in a static readonly field or register them as a singleton, and pass the same instance to every call.
Program.cs
Program.cs
The code produces the following output:
Don’t create an XmlSerializerOptions instance inside a method that runs often. A new instance works out the mapping of every type it meets again, so each call pays the first-use cost, which is many times the cost of the call itself. See Benchmarks.
When you pass no options, the serializer uses XmlSerializerOptions.Default, which is a shared instance, so calls without options are cached too. View the complete sample on GitHub

Built-in instances

Four ready-made, read-only instances cover common cases, so you often don’t need to create your own.
Program.cs
The code produces the following output:
XmlSerializerDefaults.Web is smaller than JsonSerializerDefaults.Web. It doesn’t turn on case-insensitive name matching, and it doesn’t change how numbers are read.
The strict defaults change reading, which is why the third line matches the first. The one exception for writing is RespectNullableAnnotations: a member annotated non-nullable that holds null throws when it is written. The following example reads three documents with the general defaults and then with the strict ones.
Program.cs
The code produces the following output:
With the general defaults, a repeated element overwrites the earlier value, a comment is skipped, and an unknown element is skipped. Use the strict defaults when the XML comes from outside your control and a surprise should be an error.

Start from defaults or copy an instance

To change one setting of a set of defaults, pass an XmlSerializerDefaults value to the constructor and set what you need. To derive a variation of options you already have, use the copy constructor.
Program.cs
The code produces the following output:
The copy has every setting, the converters, and the type info resolver of the source. It can be changed even when the source is read-only, and it shares none of the source’s cached type information, so it pays the first-use cost again. Make copies at startup, not per call.

Options become read-only

An instance becomes read-only the first time it is used to serialize or deserialize, or when you call MakeReadOnly. After that, every setter throws an InvalidOperationException, and so does changing the Converters list. IsReadOnly tells you which state an instance is in.
Program.cs
The code produces the following output:
The reason is the cache. Type information that was built under one set of options would be wrong under another, so the settings are locked as soon as any exists. Call MakeReadOnly yourself when you hand an instance to other code and want to be sure nobody changes it before its first use. A read-only instance is meant to be shared. See Thread safety.

Default values

The following sample prints the value of each option on a new instance. The test for the sample compares the whole output, so this list can’t drift from the code.
Program.cs
The code produces the following output:
For the sizes and limits, a value of 0 means “use the built-in default”. The tables that follow give the value that is then used.

Options reference

The tables in this section group the options by what they affect. Each row gives the value on a new instance.

Output

These options take effect when writing.

Names and shape

These options decide how members map to XML. They apply to writing and reading alike. See Customize names and shape and Serialize collections.

Which members are included

See Ignore properties and Handle null values.

Reading

These options take effect when reading. See Deserialize and Require properties.

Values

Limits

The limits protect against oversized or hostile input. See Security.

Type metadata

See also

Serialize

Write .NET objects as XML.

Deserialize

Read XML into .NET objects.

Thread safety

Share options and other types between threads.

XmlSerializerOptions

API reference for every property.