Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.SerializationSyntax
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
AnXmlReferenceResolver 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.
Considerations
- A custom resolver takes effect when reading and when writing. When writing, the identifier
GetReferencereturns is written as theidattribute of an object’s first appearance and as therefattribute of each later one. - When writing,
GetReferencecan 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.Instancedoes 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
AddReferenceafter the constructor runs. Arefto it from inside its own content can’t be resolved.
Constructors
.ctor Inherited
Inherited from
objectSyntax
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
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Inherited Virtual
Inherited from
objectSyntax
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 anid 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
objectSyntax
Returns
Type:System.Type
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
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
objectSyntax
Returns
Type:string?
Related APIs
- CloudNimble.Text.Xml.Serialization.XmlReferenceHandler
- CloudNimble.Text.Xml.XmlSerializerOptions
- CloudNimble.Text.Xml.XmlNamespaces