Skip to main content

Definition

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

Syntax

Summary

Defines the core behavior of preserving references on serialization and deserialization and provides a base for resolvers that map objects to reference identifiers during one operation.

Usage

An XmlReferenceResolver maps objects to identifiers and back for one call to Serialize or Deserialize. The serializer gets it from XmlReferenceHandler.CreateResolver. Derive from both classes to control how the identifiers in a document are resolved. The class corresponds to ReferenceResolver in System.Text.Json. The following resolver keeps the objects in a dictionary that its handler owns, so the caller can look them up after the document has been read.
Reading a document with this handler resolves its references and fills the dictionary:
The code produces the following output:
View the complete sample on GitHub

Considerations

  • A custom resolver takes effect when reading and when writing. When writing, the identifier GetReference returns is written as the id attribute of an object’s first appearance and as the ref attribute of each later one.
  • When writing, GetReference can be called twice for one object: once by the member that holds it and once when the object itself is written. Return the same identifier both times.
  • Compare objects by reference, not with Equals. Two different objects that happen to be equal must not share an identifier. ReferenceEqualityComparer.Instance does this for a dictionary or a set.
  • A resolver is used for one operation and isn’t called from more than one thread during it. If your handler shares state between resolvers, as the example does, that state needs its own protection when the options are used from several threads.
  • An object that is created through a constructor with parameters is passed to AddReference after the constructor runs. A ref to it from inside its own content can’t be resolved.

Constructors

.ctor Inherited

Inherited from object

Syntax

Methods

AddReference Abstract

When overridden in a derived class, adds an entry to the bag of references by using the specified identifier and value.

Syntax

Parameters

Remarks

The serializer calls this method when it reads an element that carries a reference identifier.

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

GetReference Abstract

When overridden in a derived class, gets the reference identifier of the specified value if one exists; otherwise, assigns a new identifier.

Syntax

Parameters

Returns

Type: string The reference identifier for value.

Remarks

The serializer calls this method before it writes an object, to decide whether to write the object or a reference to it. For a new object, it writes the returned identifier as an id attribute. For an object that already exists, it writes the identifier as a ref attribute on an empty element. When the identifier is an empty string, it writes no id, and writes xsi:nil="true" in place of an object that already exists.

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

ResolveReference Abstract

When overridden in a derived class, returns the object related to the specified reference identifier.

Syntax

Parameters

Returns

Type: object? The object that was added under referenceId, or null if there is none.

Remarks

The serializer calls this method when it reads an element that refers to another object by identifier.

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.Serialization.XmlReferenceHandler
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlNamespaces