Skip to main content

Definition

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

Syntax

Summary

Provides XML serialization-related metadata about a type.

Usage

An XmlTypeInfo is the serializer’s contract for one type: the converter that reads and writes it, the name of its document element, and the options it was built for. XmlTypeInfo<T> is the typed form you pass to XmlSerializer. You don’t create type info. You get it from one of two places:
  • A property of a source-generated context, such as OrderXmlContext.Default.Order.
  • An options instance that has a resolver, with XmlSerializerOptions.GetTypeInfo<T>(), GetTypeInfo(Type), or TryGetTypeInfo.
Pass it to an XmlSerializer overload. These overloads use no reflection, so they’re safe in trimmed and Native AOT applications:
The classes correspond to JsonTypeInfo and JsonTypeInfo<T> in System.Text.Json.

Remarks

Instances are created by the library. This class cannot be derived from outside the library.

Type Parameters

  • T - The type that the metadata describes.

Examples

Read the contract of a type

The following example lists the members of Order as the serializer maps them. The type info comes from a source-generated context, which builds it from generated metadata. A context in the default mode gives the same list.
The code produces the following output:
View the complete sample on GitHub

Considerations

  • Type info is read-only. Every property has a getter only. Unlike JsonTypeInfo, you can’t add, remove, or change members to customize a contract. Change the mapping with attributes, options, or a custom converter.
  • You can’t derive from it or construct it. The constructors aren’t accessible outside the library.
  • Properties lists the members of a type mapped as an object. Type info from the reflection resolver, or from a source-generated context in the default mode or in Metadata mode, lists one XmlPropertyInfo for each member in the order they’re written. Type info for a simple value, for a custom converter, or from a context in Serialization mode has an empty list.
  • CreateObject is null unless the type is mapped as an object and is created with no arguments. It’s null for a type that is created through a constructor with parameters, and for type info from a context in Serialization mode.
  • Type info is bound to its Options. The serializer uses those options when you pass the type info, whatever other options exist. Type info from XmlSerializerOptions.GetTypeInfo is cached on that instance, and asking for it makes the instance read-only.
  • It’s safe to share. Keep and reuse type info across threads, as you would the options or the context it came from.

Constructors

.ctor Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Creates type info.

Syntax

Parameters

.ctor Inherited

Inherited from object

Syntax

Properties

Converter

Gets the converter that reads and writes values of type T.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlConverter<T> The converter associated with the type.

Converter Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the XmlConverter associated with the current type.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlConverter The converter that reads and writes values of XmlTypeInfo.Type.

CreateObject Inherited Virtual

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets a parameterless factory that creates an instance of XmlTypeInfo.Type on deserialization.

Syntax

Property Value

Type: System.Func<object>? A factory that creates an instance of XmlTypeInfo.Type, or null if the type is not mapped as an object or is created through a constructor that has parameters.

IsNillable Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets a value that indicates whether a value of XmlTypeInfo.Type can be null.

Syntax

Property Value

Type: bool true if XmlTypeInfo.Type is a reference type or a Nullable1](https://learn.microsoft.com/dotnet/api/system.nullable-1); otherwise, [false`.

Remarks

Only a nillable type can be read from an element marked xsi:nil="true" or written from a null value. A null value that is written is an element marked xsi:nil="true".

Options Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the XmlSerializerOptions value associated with the current XmlTypeInfo instance.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlSerializerOptions The options that the metadata was created for.

Properties Inherited Virtual

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the list of XmlPropertyInfo metadata corresponding to the current type.

Syntax

Property Value

Type: System.Collections.Generic.IList<CloudNimble.Text.Xml.Serialization.XmlPropertyInfo> The serialized members of an object type, in the order they are written. The list is empty for a type that is not mapped as an object, and for a type that a context generated in XmlSourceGenerationMode.Serialization mode.

ResumableBinding Inherited Virtual

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the binding of an object that the built-in object converter reads, which is what a read from a stream can stop in the middle of and go on with; otherwise null.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlObjectBinding?

RootLeafCloseTag Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the whole end tag of the element named for XmlTypeInfo.Type, </name>, or null when the name has a namespace or is not a short ASCII name.

Syntax

Property Value

Type: byte[]?

RootLeafOpenTag Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the whole start tag of the element named for XmlTypeInfo.Type, <name>, or null when the name has a namespace or is not a short ASCII name.

Syntax

Property Value

Type: byte[]?

RootName Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the name of the document element that a value of XmlTypeInfo.Type is serialized as.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlName The name of the document element.

SequenceElementName Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the UTF-8 name of the element that holds a sequence of values of XmlTypeInfo.Type. It is found through the attributes of the type, so it is found once and kept.

Syntax

Property Value

Type: byte[]

Type Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the Type for which the XML serialization contract is defined.

Syntax

Property Value

Type: System.Type The type that the metadata describes.

Methods

CheckRoot Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Checks that the start tag reader is on is the document element of XmlTypeInfo.Type.

Syntax

Parameters

CreateRootName Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Builds the document element name of type.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlName The local name from XmlRootAttribute.ElementName, or the XML name of the type under the element naming policy when the attribute gives none; in the namespace from XmlRootAttribute.Namespace, or XmlSerializerOptions.DefaultNamespace when the attribute gives none.

Remarks

Every kind of type info uses this one rule, so a type has the same document element whether the reflection resolver, a generated context, or a converter of the caller’s reads and writes it.

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

IsNilElement Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Returns whether the start tag the reader is on is marked xsi:nil, so that the value is null.

Syntax

Parameters

Returns

Type: bool true when the element stands for null.

Exceptions

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReadElementAsObject Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Reads the element the reader is on, whatever its name, and returns the value boxed.

Syntax

Parameters

Returns

Type: object? The value.

ReadRootAsObject Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Reads the document element of XmlTypeInfo.Type and returns the value boxed.

Syntax

Parameters

Returns

Type: object? The value.

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

RootLeafTags Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Gets the tags of the element named for XmlTypeInfo.Type, built ahead of time, for a value that is written without an element frame.

Syntax

Parameters

Returns

Type: byte[]? The whole start tag, <name>, or null when the writer is not inside an element or the name cannot be written from bytes built ahead of time.

Remarks

For an item of a collection that is the document root. The caller writes exactly one value.

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?

TryWriteStartDocumentElement Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Starts an element as the document element of XmlTypeInfo.Type, with the namespaces of XmlSerializerOptions.Namespaces and of the type’s XmlNamespaceDeclarationAttributes declared on it, when there are any and the writer is at the document level.

Syntax

Parameters

Returns

Type: bool false when nothing was written, and the caller starts the element itself.

Exceptions

Remarks

The name is a parameter because a document element is not always named for the type: a value of a derived type is written under the derived type’s name, and a sequence under a name of its own.

WriteContentAsObject Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Writes a boxed value as element or attribute content, without a wrapping start tag.

Syntax

Parameters

WriteNullRoot Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Writes a null value as the document element.

Syntax

Parameters

WriteRootAsObject Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Writes a boxed value as the document element.

Syntax

Parameters

WriteStartRoot Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Writes the start tag of the element named for XmlTypeInfo.Type, declaring its namespace when it has one. When it is the document element, the namespaces of XmlSerializerOptions.Namespaces and of the type’s XmlNamespaceDeclarationAttributes are declared on it.

Syntax

Parameters

Exceptions

WriteStartRoot Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlTypeInfo
Writes the start tag of an element in the namespace of XmlTypeInfo.Type under another local name: the element that holds a sequence of values of the type.

Syntax

Parameters

Exceptions

  • CloudNimble.Text.Xml.Serialization.XmlPropertyInfo
  • CloudNimble.Text.Xml.Serialization.XmlSerializerContext
  • CloudNimble.Text.Xml.Serialization.IXmlTypeInfoResolver
  • CloudNimble.Text.Xml.Serialization.DefaultXmlTypeInfoResolver
  • CloudNimble.Text.Xml.Serialization.XmlConverter
  • CloudNimble.Text.Xml.XmlSerializer
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlName