Skip to main content

Definition

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

Syntax

Summary

Provides functionality to serialize objects or value types to XML and to deserialize XML into objects or value types. This class cannot be inherited.

Usage

XmlSerializer converts .NET objects to XML and back. It is a static class with the same shape as JsonSerializer in System.Text.Json: there is no instance to create or cache, and every method takes the value or the XML plus an optional XmlSerializerOptions. Call Serialize to get a string:
Call Deserialize<TValue> to turn XML into an object:
The methods come in families. Each family has overloads for reflection (XmlSerializerOptions), for a type known only at run time (Type), and for source generation (XmlTypeInfo<TValue> or XmlSerializerContext). For a walk through each destination and source, see Serialize and Deserialize.

Remarks

This class is the XML counterpart of System.Text.Json.JsonSerializer. Every overload resolves an XmlTypeInfo and then goes through one of the internal Read or Write methods in this file. All reading is done by Utf8XmlReader and all writing by Utf8XmlWriter.

Examples

The following examples come from the serialization samples, and each shows the output the code produces.

Serialize an object graph with indentation

The code produces the following output:

Serialize to UTF-8 bytes

The code produces the following output:
View the complete sample on GitHub

Deserialize from a string

The code produces the following output:

Handle XML that doesn’t fit the type

The code produces the following output:
View the complete sample on GitHub

Best Practices

  • Reuse the options. Keep one XmlSerializerOptions instance in a static readonly field and pass it to every call. The type metadata is cached on the instance. See Configure XmlSerializerOptions.
  • Write UTF-8 when the destination is bytes. Use SerializeToUtf8Bytes or the IBufferWriter<byte> and Stream overloads instead of creating a string and encoding it. Read UTF-8 with the ReadOnlySpan<byte> and Stream overloads for the same reason.
  • A value held as object is written as the type it has. Serialize<object>(value) and Serialize(value, value.GetType(), options) write the same document. Reading is different: Deserialize<object> returns an XmlElement, because a document doesn’t record the type. Name the type when you read.
  • Use the source-generation overloads for trimmed and Native AOT applications. The overloads that take an XmlTypeInfo<TValue> or an XmlSerializerContext don’t use reflection. See Use source generation.
  • Tighten the options for XML you don’t control. Start from XmlSerializerOptions.Strict and set MaxDocumentByteCount. See Security.
  • Dispose what SerializeToDocument returns. The XmlDocument holds pooled memory.
  • Catch XmlException around Deserialize. It reports XML that isn’t well formed, values that can’t be converted, missing required members, and unmapped members when those are disallowed.

Patterns

XmlSerializer works together with the reader, the writer, and the document types, so an object can be one part of a larger XML document.

Embed serialized objects in a document you write yourself

Open a Utf8XmlWriter, write the parts that aren’t objects, and let the serializer write each object as one element.
The code produces the following output:

Deserialize the interesting elements of a large document

Walk the document with a Utf8XmlReader and hand the serializer only the elements you want as objects. The reader is left on the end tag of the element that was read.
The code produces the following output:

Query first, then map

Parse with XmlDocument, find an element, and deserialize that element.
The code produces the following output:

Considerations

Keep the following in mind when you choose overloads and when you compare XmlSerializer with the serializers you already know.

Reflection and trimming

The overloads that take XmlSerializerOptions find members and constructors through reflection. They are annotated with RequiresUnreferencedCode and RequiresDynamicCode, so the compiler warns when a trimmed or Native AOT application calls them. The overloads that take an XmlTypeInfo<TValue> or an XmlSerializerContext aren’t annotated. Options that have no resolver are given the reflection resolver by the first of these overloads that uses them, when XmlSerializer.IsReflectionEnabledByDefault is true. When it is false and no resolver is set, the reflection overloads throw an InvalidOperationException.

Cost of the first call

The first call for a type with a given options instance builds the type’s XML mapping and caches it on the instance. That call costs many times what later calls with the same instance cost, so create the options once and reuse them. See Benchmarks.

Buffering

  • Serialize to a string or a byte[], and SerializeAsync to a Stream, build the whole document in a pooled buffer first. SerializeAsync then writes it to the stream in one asynchronous write.
  • Deserialize and DeserializeAsync from a Stream or a PipeReader read to the end into a pooled buffer before parsing. MaxDocumentByteCount limits how much is read.
  • Deserialize from an XmlElement or an XmlNode writes that element to a pooled buffer and reads it back.
  • To process a large document without holding it all in memory, use SerializeAsyncEnumerable and DeserializeAsyncEnumerable. See Streaming.

Differences from System.Text.Json

  • Serialize<object>(value) writes the value as the type it has, and names the document element after that type. Deserialize<object> returns an XmlElement.
  • RespectRequiredConstructorParameters defaults to true, so a document that lacks a value for a constructor parameter with no default value is rejected. See Immutable types.
  • A null member is written as an element marked xsi:nil="true" by default, because XML has no null literal.
  • A collection is written as repeated elements, and a document has exactly one root element.

Differences from System.Xml.Serialization.XmlSerializer

  • There is no instance to construct and cache per type.
  • Only the attributes in CloudNimble.Text.Xml.Serialization are read. Attributes from System.Xml.Serialization, IXmlSerializable, and data contract attributes are ignored.
  • Collections have no wrapper element unless you add [XmlArray] or set DefaultCollectionMapping to Wrapped. XmlSerializerOptions.Bcl sets that and the other settings that write what the old serializer writes.
  • Input and output are UTF-8. The library doesn’t reference System.Xml.
See Migrate from XmlSerializer.

Exceptions

Deserialize throws an XmlException for XML that isn’t well formed and for XML that doesn’t satisfy the type. A type whose mapping is not valid, such as one with [XmlAttribute] on an object member or with two members that share an element name, throws a NotSupportedException or an InvalidOperationException the first time the type is used, for serializing as well as deserializing.

Thread safety

The methods are static and keep no state between calls. See Thread safety.

Properties

IsReflectionEnabledByDefault

Gets a value that indicates whether unconfigured XmlSerializerOptions instances use the reflection-based DefaultXmlTypeInfoResolver.

Syntax

Property Value

Type: bool true if options with no XmlSerializerOptions.TypeInfoResolver fall back to DefaultXmlTypeInfoResolver; otherwise, false. The default is true.

Remarks

The value is true unless the CloudNimble.Text.Xml.XmlSerializer.IsReflectionEnabledByDefault feature switch is false. Native AOT consumers set that switch to false so the trimmer can drop the reflection resolver.

Methods

Deserialize

Parses the text representing a single XML document into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the text representing a single XML document into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the text representing a single XML document into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the text representing a single XML document into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the text representing a single XML document into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the UTF-16 text span representing a single XML document into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-16 text span representing a single XML document into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-16 text span representing a single XML document into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-16 text span representing a single XML document into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the UTF-16 text span representing a single XML document into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded text representing a single XML document into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded text representing a single XML document into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded text representing a single XML document into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded text representing a single XML document into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded text representing a single XML document into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded byte sequence representing a single XML document into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded byte sequence representing a single XML document into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded byte sequence representing a single XML document into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded byte sequence representing a single XML document into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Parses the UTF-8 encoded byte sequence representing a single XML document into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Reads one XML element from the provided reader into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Reads one XML element from the provided reader into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Reads one XML element from the provided reader into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Reads one XML element from the provided reader into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Reads one XML element from the provided reader into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Reads the UTF-8 encoded text representing a single XML document from a stream into an instance of the type specified by a generic type parameter, using the specified options. The stream is read to completion.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Reads the UTF-8 encoded text representing a single XML document from a stream into a TValue, using the specified type metadata. The stream is read to completion.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Reads the UTF-8 encoded text representing a single XML document from a stream into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context. The stream is read to completion.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Reads the UTF-8 encoded text representing a single XML document from a stream into an instance of a specified type, using the specified options. The stream is read to completion.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Reads the UTF-8 encoded text representing a single XML document from a stream into an instance of a specified type, using type metadata from the specified serializer context. The stream is read to completion.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Converts the root element of an XmlDocument into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the root element of an XmlDocument into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the root element of an XmlDocument into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the root element of an XmlDocument into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Converts the root element of an XmlDocument into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Converts the XmlElement representing a single XML value into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the XmlElement representing a single XML value into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the XmlElement representing a single XML value into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the XmlElement representing a single XML value into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Converts the XmlElement representing a single XML value into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Converts the XmlNode representing a single XML value into an instance of the type specified by a generic type parameter, using the specified options.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the XmlNode representing a single XML value into a TValue, using the specified type metadata.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the XmlNode representing a single XML value into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: TValue? A TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

Deserialize

Converts the XmlNode representing a single XML value into an instance of a specified type, using the specified options.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

Deserialize

Converts the XmlNode representing a single XML value into an instance of a specified type, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: object? A returnType representation of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a stream into an instance of the type specified by a generic type parameter, using the specified options. The stream is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<TValue?> A task object that, when awaited, produces a TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a stream into a TValue, using the specified type metadata. The stream is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<TValue?> A task object that, when awaited, produces a TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a stream into an instance of the type specified by a generic type parameter, using type metadata from the specified serializer context. The stream is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<TValue?> A task object that, when awaited, produces a TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a stream into an instance of a specified type, using the specified options. The stream is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<object?> A task object that, when awaited, produces a returnType representation of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a stream into an instance of a specified type, using type metadata from the specified serializer context. The stream is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<object?> A task object that, when awaited, produces a returnType representation of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a pipe into an instance of the type specified by a generic type parameter, using the specified options. The pipe is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<TValue?> A task object that, when awaited, produces a TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

DeserializeAsync

Asynchronously reads the UTF-8 encoded text representing a single XML document from a pipe into a TValue, using the specified type metadata. The pipe is read to completion.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.ValueTask<TValue?> A task object that, when awaited, produces a TValue representation of the XML value.

Type Parameters

  • TValue - The target type of the XML value.

Exceptions

DeserializeAsyncEnumerable

Wraps the UTF-8 encoded text in a stream into an IAsyncEnumerable`1 that deserializes each child of the document element in a streaming manner, using the specified options.

Syntax

Parameters

Returns

Type: System.Collections.Generic.IAsyncEnumerable<TValue?> An IAsyncEnumerable`1 representation of the children of the document element.

Type Parameters

  • TValue - The element type to deserialize asynchronously.

Exceptions

DeserializeAsyncEnumerable

Wraps the UTF-8 encoded text in a stream into an IAsyncEnumerable`1 that deserializes either a sequence of documents or the children of one document element in a streaming manner, using the specified options.

Syntax

Parameters

Returns

Type: System.Collections.Generic.IAsyncEnumerable<TValue?> An IAsyncEnumerable`1 representation of the values in the stream.

Type Parameters

  • TValue - The element type to deserialize asynchronously.

Exceptions

DeserializeAsyncEnumerable

Wraps the UTF-8 encoded text in a stream into an IAsyncEnumerable`1 that deserializes each child of the document element in a streaming manner, using the specified type metadata.

Syntax

Parameters

Returns

Type: System.Collections.Generic.IAsyncEnumerable<TValue?> An IAsyncEnumerable`1 representation of the children of the document element.

Type Parameters

  • TValue - The element type to deserialize asynchronously.

Exceptions

DeserializeAsyncEnumerable

Wraps the UTF-8 encoded text in a stream into an IAsyncEnumerable`1 that deserializes either a sequence of documents or the children of one document element in a streaming manner, using the specified type metadata.

Syntax

Parameters

Returns

Type: System.Collections.Generic.IAsyncEnumerable<TValue?> An IAsyncEnumerable`1 representation of the values in the stream.

Type Parameters

  • TValue - The element type to deserialize asynchronously.

Exceptions

Remarks

Values are returned as the stream delivers them. Each read from the stream is followed by every value that is now complete, so the buffer holds one read plus at most one unfinished value, not the whole stream. A malformed document fails when the bad part is reached; values before it have already been returned.

DeserializeAsyncEnumerable

Wraps the UTF-8 encoded text in a pipe into an IAsyncEnumerable`1 that deserializes either a sequence of documents or the children of one document element in a streaming manner, using the specified options.

Syntax

Parameters

Returns

Type: System.Collections.Generic.IAsyncEnumerable<TValue?> An IAsyncEnumerable`1 representation of the values in the pipe.

Type Parameters

  • TValue - The element type to deserialize asynchronously.

Exceptions

DeserializeAsyncEnumerable

Wraps the UTF-8 encoded text in a pipe into an IAsyncEnumerable`1 that deserializes each child of the document element in a streaming manner, using the specified type metadata.

Syntax

Parameters

Returns

Type: System.Collections.Generic.IAsyncEnumerable<TValue?> An IAsyncEnumerable`1 representation of the children of the document element.

Type Parameters

  • TValue - The element type to deserialize asynchronously.

Exceptions

Serialize

Converts the value of a type specified by a generic type parameter into an XML string, using the specified options.

Syntax

Parameters

Returns

Type: string An XML string representation of the value.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the provided value into an XML string, using the specified type metadata.

Syntax

Parameters

Returns

Type: string An XML string representation of the value.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the value of a type specified by a generic type parameter into an XML string, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: string An XML string representation of the value.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the value of a specified type into an XML string, using the specified options.

Syntax

Parameters

Returns

Type: string An XML string representation of the value.

Exceptions

Serialize

Converts the value of a specified type into an XML string, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: string An XML string representation of the value.

Exceptions

Serialize

Writes the XML representation of a type specified by a generic type parameter to the provided writer as one element, using the specified options.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Writes the XML representation of the provided value to the provided writer as one element, using the specified type metadata.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Writes the XML representation of a type specified by a generic type parameter to the provided writer as one element, using type metadata from the specified serializer context.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Writes the XML representation of the specified type to the provided writer as one element, using the specified options.

Syntax

Parameters

Exceptions

Serialize

Writes the XML representation of the specified type to the provided writer as one element, using type metadata from the specified serializer context.

Syntax

Parameters

Exceptions

Serialize

Converts the value of a type specified by a generic type parameter to UTF-8 encoded XML text and writes it to a stream, using the specified options.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the provided value to UTF-8 encoded XML text and writes it to a stream, using the specified type metadata.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the value of a type specified by a generic type parameter to UTF-8 encoded XML text and writes it to a stream, using type metadata from the specified serializer context.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the value of a specified type to UTF-8 encoded XML text and writes it to a stream, using the specified options.

Syntax

Parameters

Exceptions

Serialize

Converts the value of a specified type to UTF-8 encoded XML text and writes it to a stream, using type metadata from the specified serializer context.

Syntax

Parameters

Exceptions

Serialize

Converts the value of a type specified by a generic type parameter to UTF-8 encoded XML text and writes it to a buffer writer, using the specified options.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the provided value to UTF-8 encoded XML text and writes it to a buffer writer, using the specified type metadata.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the value of a type specified by a generic type parameter to UTF-8 encoded XML text and writes it to a buffer writer, using type metadata from the specified serializer context.

Syntax

Parameters

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Serialize

Converts the value of a specified type to UTF-8 encoded XML text and writes it to a buffer writer, using the specified options.

Syntax

Parameters

Exceptions

Serialize

Converts the value of a specified type to UTF-8 encoded XML text and writes it to a buffer writer, using type metadata from the specified serializer context.

Syntax

Parameters

Exceptions

SerializeAsync

Asynchronously converts a value of a type specified by a generic type parameter to UTF-8 encoded XML text and writes it to a stream, using the specified options.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeAsync

Asynchronously converts the provided value to UTF-8 encoded XML text and writes it to a stream, using the specified type metadata.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeAsync

Asynchronously converts a value of a type specified by a generic type parameter to UTF-8 encoded XML text and writes it to a stream, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeAsync

Asynchronously converts the value of a specified type to UTF-8 encoded XML text and writes it to a stream, using the specified options.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation.

Exceptions

SerializeAsync

Asynchronously converts the value of a specified type to UTF-8 encoded XML text and writes it to a stream, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation.

Exceptions

SerializeAsync

Asynchronously converts a value of a type specified by a generic type parameter to UTF-8 encoded XML text, writes it to a pipe, and flushes the pipe, using the specified options.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation. It completes when the pipe has been flushed.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

Remarks

To write to a pipe without flushing it, pass the PipeWriter to the [IBufferWriter1](https://learn.microsoft.com/dotnet/api/system.buffers.ibufferwriter-1) overload of Serialize`.

SerializeAsyncEnumerable

Asynchronously converts a sequence of values to UTF-8 encoded XML text and writes them to a stream as the children of one ArrayOf* document element, using the specified options.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation. It completes when the document has been written.

Type Parameters

  • TValue - The type of the values to serialize.

Exceptions

SerializeAsyncEnumerable

Asynchronously converts a sequence of values to UTF-8 encoded XML text and writes them to a stream, either as separate documents or as the children of one document element, using the specified options.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation. It completes when every value has been written.

Type Parameters

  • TValue - The type of the values to serialize.

Exceptions

SerializeAsyncEnumerable

Asynchronously converts a sequence of values to UTF-8 encoded XML text and writes them to a stream, either as separate documents or as the children of one document element, using the specified type metadata.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation. It completes when every value has been written.

Type Parameters

  • TValue - The type of the values to serialize.

Exceptions

Remarks

The ArrayOf* document element is named ArrayOf followed by the local name of the root element of TValue, and it is in the same namespace as that element. When the options have a XmlSerializerOptions.ReferenceHandler, the values of one document share one set of references, and each top-level value has its own. The stream is written to between values, never while one value is half written, unless a single value is larger than the writer’s buffer. If a value fails to serialize, what was buffered is dropped; values already written to the stream stay there.

SerializeAsyncEnumerable

Asynchronously converts a sequence of values to UTF-8 encoded XML text, writes them to a pipe, either as separate documents or as the children of one document element, and flushes the pipe, using the specified options.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task that represents the asynchronous write operation. It completes when the pipe has been flushed.

Type Parameters

  • TValue - The type of the values to serialize.

Exceptions

SerializeToDocument

Converts the value of a type specified by a generic type parameter into an XmlDocument, using the specified options.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlDocument An XmlDocument representation of the value. The caller is responsible for disposing it.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToDocument

Converts the provided value into an XmlDocument, using the specified type metadata.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlDocument An XmlDocument representation of the value. The caller is responsible for disposing it.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToDocument

Converts the value of a type specified by a generic type parameter into an XmlDocument, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlDocument An XmlDocument representation of the value. The caller is responsible for disposing it.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToDocument

Converts the value of a specified type into an XmlDocument, using the specified options.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlDocument An XmlDocument representation of the value. The caller is responsible for disposing it.

Exceptions

SerializeToDocument

Converts the value of a specified type into an XmlDocument, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlDocument An XmlDocument representation of the value. The caller is responsible for disposing it.

Exceptions

SerializeToElement

Converts the value of a type specified by a generic type parameter into an XmlElement, using the specified options.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement representation of the value. It is the root element of a new document.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToElement

Converts the provided value into an XmlElement, using the specified type metadata.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement representation of the value. It is the root element of a new document.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToElement

Converts the value of a type specified by a generic type parameter into an XmlElement, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement representation of the value. It is the root element of a new document.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToElement

Converts the value of a specified type into an XmlElement, using the specified options.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement representation of the value. It is the root element of a new document.

Exceptions

SerializeToElement

Converts the value of a specified type into an XmlElement, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement representation of the value. It is the root element of a new document.

Exceptions

SerializeToNode

Converts the value of a type specified by a generic type parameter into a mutable XmlNode, using the specified options.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode? An XmlNode representation of the value. It is the root node of a new tree.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToNode

Converts the provided value into a mutable XmlNode, using the specified type metadata.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode? An XmlNode representation of the value. It is the root node of a new tree.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToNode

Converts the value of a type specified by a generic type parameter into a mutable XmlNode, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode? An XmlNode representation of the value. It is the root node of a new tree.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToNode

Converts the value of a specified type into a mutable XmlNode, using the specified options.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode? An XmlNode representation of the value. It is the root node of a new tree.

Exceptions

SerializeToNode

Converts the value of a specified type into a mutable XmlNode, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode? An XmlNode representation of the value. It is the root node of a new tree.

Exceptions

SerializeToUtf8Bytes

Converts the value of a type specified by a generic type parameter into an XML string, encoded as UTF-8 bytes, using the specified options.

Syntax

Parameters

Returns

Type: byte[] An XML string representation of the value, encoded as UTF-8 bytes.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToUtf8Bytes

Converts the provided value into an XML string, encoded as UTF-8 bytes, using the specified type metadata.

Syntax

Parameters

Returns

Type: byte[] An XML string representation of the value, encoded as UTF-8 bytes.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToUtf8Bytes

Converts the value of a type specified by a generic type parameter into an XML string, encoded as UTF-8 bytes, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: byte[] An XML string representation of the value, encoded as UTF-8 bytes.

Type Parameters

  • TValue - The type of the value to serialize.

Exceptions

SerializeToUtf8Bytes

Converts the value of a specified type into an XML string, encoded as UTF-8 bytes, using the specified options.

Syntax

Parameters

Returns

Type: byte[] An XML string representation of the value, encoded as UTF-8 bytes.

Exceptions

SerializeToUtf8Bytes

Converts the value of a specified type into an XML string, encoded as UTF-8 bytes, using type metadata from the specified serializer context.

Syntax

Parameters

Returns

Type: byte[] An XML string representation of the value, encoded as UTF-8 bytes.

Exceptions

  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlSerializerDefaults
  • CloudNimble.Text.Xml.Serialization.XmlSerializerContext
  • CloudNimble.Text.Xml.Serialization.XmlTypeInfo
  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlElement
  • CloudNimble.Text.Xml.Nodes.XmlNode
  • CloudNimble.Text.Xml.XmlException