Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.SerializationSyntax
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
SetXmlSerializerOptions.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.
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. WithPreserve, the cycle is written once and restored when the document is read.
Ignore a cycle
WithIgnoreCycles, the same graph is written without identifiers, and the member that would close the cycle is nil.
Best Practices
- Leave
ReferenceHandlerunset unless the graph has shared objects or cycles. With no handler, the serializer does no reference tracking. - Use
Preservewhen both ends use XmlEssentials and the graph must come back as it was. - Use
IgnoreCycleswhen the document goes to a consumer that knows nothing aboutidandref, 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
refattribute is anXmlException. - If the cycle is caused by a back-reference that you don’t need in the document, such as a child’s
Parentproperty, consider[XmlIgnore]on that member instead of a handler.
Considerations
- Cost.
Preservemakes 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
Preservereplaces 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. IgnoreCyclesreplaces 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, asReferenceHandler.IgnoreCyclesdoes in System.Text.Json.- Custom handlers. A class derived from
XmlReferenceHandlersupplies the resolver for each call, for writing as well as reading. When writing, the identifiers your resolver returns fromGetReferenceare written as theidandrefattributes. - Sequences.
SerializeAsyncEnumerableandDeserializeAsyncEnumerableapply 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. WithtopLevelValues: 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
Metadatamode applies the handler as the reflection resolver does. - Thread safety.
PreserveandIgnoreCyclesare stateless singletons. Each call toSerializeorDeserializegets its own resolver, so options that use them can be shared between threads.
Constructors
.ctor Inherited
Inherited from
objectSyntax
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 byReferenceHandler.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
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
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
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Related APIs
- CloudNimble.Text.Xml.Serialization.XmlReferenceResolver
- CloudNimble.Text.Xml.XmlSerializerOptions
- CloudNimble.Text.Xml.XmlNamespaces
- CloudNimble.Text.Xml.Serialization.XmlIgnoreAttribute