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 astatic readonly field or register them as a singleton, and pass the same instance to every call.
Program.cs
Program.cs
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
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.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
Start from defaults or copy an instance
To change one setting of a set of defaults, pass anXmlSerializerDefaults value to the constructor and set what you need. To derive a variation of options you already have, use the copy constructor.
Program.cs
Options become read-only
An instance becomes read-only the first time it is used to serialize or deserialize, or when you callMakeReadOnly. 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
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
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.