XmlSerializationOptions Enumeration

Options for serializer methods of XmlSerializer class.

Definition

Namespace: KGySoft.Serialization.Xml
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
C#
[FlagsAttribute]
public enum XmlSerializationOptions

Members

None0x0000

Represents no enabled options.

With every options disabled only those types are serialized, which are guaranteed to be able to deserialized perfectly. Such types are:

  • Natively supported types – Primitive types along with their NullableT counterpart and the most common framework types. See the complete list of the natively supported types at the Remarks section of the XmlSerializer class.
  • Enum types – Please note that in safe mode deserialization they must be specified as expected custom types.
  • Type instances – Only if they are runtime types. And in safe mode deserialization the natively not supported types must be included in expected custom types.
  • IXmlSerializable instances – Types that implement the IXmlSerializable interface can be serialized.
  • Types with TypeConverter – If the converter supports serializing to and from String type.
  • Simple objects – A type can be serialized with the default options if it meets the following criteria:
    • The type has a parameterless constructor, or is a value type
    • It has only public instance fields and properties. For properties, both accessors are public. Static members are ignored.

        Note

      Compiler-generated backing fields are ignored so types with public auto properties are considered simple.
    • All fields and properties can be set, or, all read-only fields and properties are either IXmlSerializable implementations or natively supported collections.
    • None of the fields and properties are delegates.
    • The type has no instance events.

      Note

    A type can be serialized if these criteria are true for the serialized properties and fields recursively.
  • Collections – Arrays, a sort of known lists and dictionaries are supported natively as long as they use a supported comparer. See the complete list of the natively supported collections at the Remarks section of the XmlSerializer class. To support other collections you can use fallback options, for example RecursiveSerializationAsFallback.

FullyQualifiedNames0x0001

If enabled, collection elements and non binary-serialized complex objects will be identified by the assembly qualified type name; otherwise, only by full type name. Using fully qualified names makes possible to automatically load the assembly of a referenced type (unless safe mode is used on deserialization). Partial identity match is allowed, so type resolving tolerates assembly version change. When resolving non-fully qualified type names, their assembly must be loaded before the deserialization; otherwise, the type resolving will fail, even in non-safe mode.

Default state at serialization methods: Disabled

BinarySerializationAsFallback0x0002

If a type cannot be parsed natively and has no TypeConverter with String support, then enabling this option makes possible to store its content in binary format (using the BinarySerializationFormatter class) within the XML.

Though trusted collections and objects with only public read-write properties and fields can be serialized with the None options as well, using this option will cause to serialize them in binary format, too.

If both BinarySerializationAsFallback and RecursiveSerializationAsFallback options are enabled, then binary serialization has higher priority, except for properties that are marked by Content visibility, which causes the property to be serialized recursively.

Default state at serialization methods: Disabled

  Security Note

When using DeserializeSafe and DeserializeContentSafe methods, then types that occur in the binary serialization stream by name must be included in the specified expected custom types as well.
RecursiveSerializationAsFallback0x0004

If a type cannot be parsed natively, has no TypeConverter with String support or binary serialization is disabled, then enabling this option makes possible to serialize the object by serializing its public properties, fields and collection items recursively. If a property or collection element cannot be serialized, then a SerializationException will be thrown.

Properties can be marked by DesignerSerializationVisibilityAttribute with Content value to indicate that they should be serialized recursively without using this fallback option.

  Caution

Enabling this option will not guarantee that deserialization of the object will be the same as the original instance. Use this option only when serialized types can be restored by setting public properties and fields, and the type has a default constructor. To avoid circular references use DesignerSerializationVisibilityAttribute with Hidden value on back-referencing properties.

If both BinarySerializationAsFallback and RecursiveSerializationAsFallback options are enabled, then binary serialization has higher priority, except for properties that are marked by Content visibility, which causes the property to be serialized recursively.

Key and Value properties of DictionaryEntry and KeyValuePairTKey, TValue instances are always serialized recursively because those are natively supported types.

Default state at serialization methods: Disabled

CompactSerializationOfStructures0x0008

If a ValueType (struct) has no TypeConverter and contains no references, then by enabling this option the instance will be serialized in a compact binary form.

  Note

This option has higher priority than fallback options (BinarySerializationAsFallback and RecursiveSerializationAsFallback), except for DictionaryEntry and KeyValuePairTKey, TValue instances, which are always serialized recursively. This option affects only instances, which have no reference fields at all.

Default state at serialization methods: Disabled

AutoGenerateDefaultValuesAsFallback0x0010

If enabled, then members without DefaultValueAttribute defined, will be treated as if they were decorated by DefaultValueAttribute with the default value of the property type ( for reference types and bitwise zero value of value types). This causes to skip serializing members, whose value equals to the default value of their type.

Default state at serialization methods: Disabled

IgnoreDefaultValueAttribute0x0020

Ignores the originally defined DefaultValueAttribute definitions for all of the properties. This causes that all members will be serialized regardless of their values.

Default state at serialization methods: Disabled

IgnoreShouldSerialize0x0040

Ignores the presence of ShouldSerialize<PropertyName> methods for all of the members.

Default state at serialization methods: Disabled

IgnoreIXmlSerializable0x0080

If enabled, XmlSerializer ignores IXmlSerializable implementations.

Default state at serialization methods: Disabled

CompactSerializationOfPrimitiveArrays0x0100

If enabled, then array of primitive types are serialized in a single XML node instead of creating XML nodes for each element in the array.

Default state at serialization methods: Enabled

EscapeNewlineCharacters0x0200

Unless a well configured XmlWriter is used, newline characters of string or char values can be lost or changed during deserialization. This flag ensures that newline characters can be always deserialized regardless of the used NewLineHandling value of an XmlWriter.

Default state at serialization methods: Enabled

OmitCrcAttribute0x0400

When this flag is enabled, binary contents will not be protected by a CRC value. Affects CompactSerializationOfPrimitiveArrays, CompactSerializationOfStructures and BinarySerializationAsFallback flags.

Default state at serialization methods: Disabled

ExcludeFields0x0800

By default XmlSerializer includes public fields in serialization, similarly to System.Xml.Serialization.XmlSerializer. By enabling this option, only public properties will be serialized.

Default state at serialization methods: Disabled

ForcedSerializationOfReadOnlyMembersAndCollections0x1000

By default read-only properties and fields are serialized only if they are IXmlSerializable implementations or collections that can be populated. This option forces to serialize read-only fields and properties, as well as collections that are read-only and have no recognizable initializer constructor.

  Note

  • Public properties with a private setter accessor are serializable even without this option.
  • Read-only collections with recognizable collection initializer constructor are serializable even without this option.

  Caution

Enabling this option can make it possible that properties without setter accessor will not be able to deserialized. Deserialization will fail if the read-only property returns or its content cannot be restored (eg. it has a simple type or is a read-only collection). Use this option only if an object has to be serialized only for information (eg. logging) and deserialization is not necessary.

Default state at serialization methods: Disabled

IgnoreTypeForwardedFromAttribute0x2000

When both FullyQualifiedNames and this flag are enabled, then every type will be serialized with its actual assembly identity rather than considering the value of an existing TypeForwardedFromAttribute.

This flag is ignored if FullyQualifiedNames is disabled.

  Note

Enabling this flag may cause that the type will not be able to be deserialized on a different platform.

Default state at serialization methods: Disabled

IncludeRefProperties0x4000

Indicates that properties with ref return type should also be serialized, which are omitted without using this flag. It treats ref readonly properties like regular read-only properties regarding collections (see the details at the None option).

  Note

As objects with ref return type are not considered trusted types it might be needed to combine this flag with the RecursiveSerializationAsFallback option.

Default state at serialization methods: Disabled

See Also