Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Serialization Inheritance: System.Attribute

Syntax

Summary

Indicates, when placed on a type, that the type is serialized polymorphically, and specifies how its type discriminator is written. This class cannot be inherited.

Usage

Put [XmlPolymorphic] on a base class to say how its derived types are told apart in a document, and add one [XmlDerivedType<T>] for each derived type that is allowed. The pair corresponds to [JsonPolymorphic] and [JsonDerivedType] in System.Text.Json.
With the default placement, [XmlPolymorphic] itself is optional: [XmlDerivedType<T>] attributes alone give the same result. Add [XmlPolymorphic] to choose another placement or another way to handle undeclared types. See Serialize derived classes for a walkthrough.

Examples

Serialize a list of a base type

Each derived type is written under its own element name, and is read back as the same type. An object of the base type keeps the member’s element name.
The code produces the following output:

Use xsi:type and fall back to an ancestor

SportsCar derives from Car and isn’t declared, so it is written as a Car. The code produces the following output:
View the complete sample on GitHub

Best Practices

  • Declare every derived type that can appear. The default, FailSerialization, then reports a type you forgot instead of writing part of it.
  • Prefer ElementName when you control the format. It needs no extra attribute, and the document says what each element is.
  • Use Attribute or XsiType when a schema fixes the element name.
  • Choose a fallback value of UnknownDerivedTypeHandling only when losing the members of the undeclared type is acceptable.
  • Give each derived type a discriminator that is not the element name of another member of the containing type.

Considerations

  • The declared base type can be a concrete class, an abstract class, or an interface. An abstract class or an interface must list its derived types with [XmlDerivedType<T>]. When the serializer reads an element that doesn’t identify one of them, it throws XmlException, because it can’t create the declared type.
  • Derived types inherit the attribute and the list of derived types. A value whose type is the declared type, such as a Book serialized as a Book or held by a member declared as Book, is written with no discriminator. A type below the declared type is still identified, so a value of a type derived from Book is written with its discriminator.
  • InferClosedTypePolymorphism takes the derived types of a closed type from the list the compiler makes. Set to true, it infers for the type whatever XmlSerializerOptions.InferClosedTypePolymorphism says; set to false, it turns inference off for the type. On a type that isn’t closed, true throws an InvalidOperationException the first time the type is used. A closed type needs C# 15 and .NET 11.
  • A source-generated context reads and writes derived types as the reflection resolver does. Listing the base type on the context also covers the derived types it declares.
  • The mapping is built once for each XmlSerializerOptions instance and cached. There is no API to add derived types at run time.

Constructors

.ctor

Syntax

.ctor Inherited

Inherited from System.Attribute

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

DiscriminatorPlacement

Gets or sets where the type discriminator is written.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlDiscriminatorPlacement One of the enumeration values that specifies where the type discriminator is written. The default is XmlDiscriminatorPlacement.ElementName.

InferClosedTypePolymorphism

Gets or sets a value that indicates whether the derived types of a closed type are inferred.

Syntax

Property Value

Type: bool true if the derived types of the type are taken from its closed hierarchy; false if they are not, whatever the options say. When the property is not set, XmlSerializerOptions.InferClosedTypePolymorphism decides. The getter returns false for a property that is not set.

Remarks

A closed type is a class declared with the closed modifier, which needs C# 15 and .NET 11. Its derived types are all in its own assembly, and the compiler lists them. With this property, a derived type is written and read as if the type had an XmlDerivedTypeAttribute for it, with the name of the derived type as its discriminator. Derived types that are declared with XmlDerivedTypeAttribute are the whole list: nothing is inferred for a type that declares one. Setting the property to true on a type that is not closed throws an InvalidOperationException the first time the type is used.

TypeDiscriminatorPropertyName

Gets or sets the local name of the attribute that holds the type discriminator.

Syntax

Property Value

Type: string? The local name of the discriminator attribute. The default is null.

Remarks

The name is required when XmlPolymorphicAttribute.DiscriminatorPlacement is XmlDiscriminatorPlacement.Attribute: the reflection-based serializer throws a NotSupportedException for the annotated type if the value is null, empty, or white space. The name is not used for the other placements.

TypeId Inherited Virtual

Inherited from System.Attribute

Syntax

Property Value

Type: object

UnknownDerivedTypeHandling

Gets or sets the behavior when serializing or deserializing a derived type that is not declared.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlUnknownDerivedTypeHandling One of the enumeration values that specifies how an undeclared derived type is handled. The default is XmlUnknownDerivedTypeHandling.FailSerialization.

Methods

Equals Override

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetHashCode Override

Inherited from System.Attribute

Syntax

Returns

Type: int

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

IsDefaultAttribute Inherited Virtual

Inherited from System.Attribute

Syntax

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

Match Inherited Virtual

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.Serialization.XmlDerivedTypeAttribute
  • CloudNimble.Text.Xml.Serialization.XmlUnknownDerivedTypeHandling
  • CloudNimble.Text.Xml.XmlDiscriminatorPlacement
  • CloudNimble.Text.Xml.XmlSerializer
  • CloudNimble.Text.Xml.XmlSerializerOptions