Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Serialization

Syntax

Summary

Defines how XmlSerializer deals with references on serialization and deserialization, and provides a base for handlers that create an XmlReferenceResolver for each operation.

Usage

Set XmlSerializerOptions.ReferenceHandler to tell the serializer what to do when an object graph holds the same object more than once. The property is null by default, which means every object is written each time it is met. The class corresponds to ReferenceHandler in System.Text.Json.
The id and ref attributes are in the namespace urn:CloudNimble.Text.Xml:refs, which is available as XmlNamespaces.Refs. See Preserve references for the full article.

Examples

Preserve a cycle

An employee refers to a manager, and the manager lists the employee as a direct report. With Preserve, the cycle is written once and restored when the document is read.
The code produces the following output:

Ignore a cycle

With IgnoreCycles, the same graph is written without identifiers, and the member that would close the cycle is nil.
The code produces the following output:
View the complete sample on GitHub

Best Practices

  • Leave ReferenceHandler unset unless the graph has shared objects or cycles. With no handler, the serializer does no reference tracking.
  • Use Preserve when both ends use XmlEssentials and the graph must come back as it was.
  • Use IgnoreCycles when the document goes to a consumer that knows nothing about id and ref, and losing the back-reference that closes each cycle is acceptable. A shared object that forms no cycle is written in full each time, and reads back as separate objects.
  • Read a document with the handler that wrote it. Without a handler, a ref attribute is an XmlException.
  • If the cycle is caused by a back-reference that you don’t need in the document, such as a child’s Parent property, consider [XmlIgnore] on that member instead of a handler.

Considerations

  • Cost. Preserve makes writing slower than no handler: each object costs a dictionary lookup, an identifier string, and a namespaced attribute.
  • What gets an identifier. Objects with members do. Strings, numbers, other simple values, and collections do not.
  • Where Preserve replaces an object. A repeated object is replaced by a reference wherever it appears: as the document element, as the value of a member, and as an item of a collection.
  • IgnoreCycles replaces only a cycle. It writes nil in place of an object that is reached again from inside itself. An object held in two places without a cycle, such as two members or two items of a list, is written in full each time, as ReferenceHandler.IgnoreCycles does in System.Text.Json.
  • Custom handlers. A class derived from XmlReferenceHandler supplies the resolver for each call, for writing as well as reading. When writing, the identifiers your resolver returns from GetReference are written as the id and ref attributes.
  • Sequences. SerializeAsyncEnumerable and DeserializeAsyncEnumerable apply the handler. In the wrapped form, the values of one document share one set of references, so an object that two values hold is written once. With topLevelValues: true, each value is a document of its own, with references of its own.
  • Source generation. A source-generated context in the default mode or in Metadata mode applies the handler as the reflection resolver does.
  • Thread safety. Preserve and IgnoreCycles are stateless singletons. Each call to Serialize or Deserialize gets its own resolver, so options that use them can be shared between threads.

Constructors

.ctor Inherited

Inherited from object

Syntax

Properties

IgnoreCycles

Gets a handler that ignores an object when it is reached again from inside itself during serialization.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlReferenceHandler A handler that writes an empty element marked xsi:nil="true" in place of an object that is still being written, which is the object that closes a cycle.

Remarks

Only a cycle is replaced. An object that is held in two places without a cycle, such as two members or two items of a list, is written in full each time, as it is by ReferenceHandler.IgnoreCycles in System.Text.Json.

Preserve

Gets a handler that preserves references by writing identifier attributes on serialization and honoring them on deserialization.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlReferenceHandler A handler that writes an id attribute on each object and a ref attribute in place of an object that was already written. Both attributes are in the urn:CloudNimble.Text.Xml:refs namespace.

Remarks

A repeated object is replaced wherever it appears: as the document element, as the value of a member, and as an item of a collection. Value types and collections have no identifier.

Methods

CreateResolver Abstract

When overridden in a derived class, returns the XmlReferenceResolver used for one serialization or deserialization call.

Syntax

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlReferenceResolver The resolver to use for the operation.

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

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.XmlReferenceResolver
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlNamespaces
  • CloudNimble.Text.Xml.Serialization.XmlIgnoreAttribute