Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.XmlSyntax
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:
Deserialize<TValue> to turn XML into an object:
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 ofSystem.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
Serialize to UTF-8 bytes
Deserialize from a string
Handle XML that doesn’t fit the type
Best Practices
- Reuse the options. Keep one
XmlSerializerOptionsinstance in astatic readonlyfield 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
SerializeToUtf8Bytesor theIBufferWriter<byte>andStreamoverloads instead of creating astringand encoding it. Read UTF-8 with theReadOnlySpan<byte>andStreamoverloads for the same reason. - A value held as
objectis written as the type it has.Serialize<object>(value)andSerialize(value, value.GetType(), options)write the same document. Reading is different:Deserialize<object>returns anXmlElement, 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 anXmlSerializerContextdon’t use reflection. See Use source generation. - Tighten the options for XML you don’t control. Start from
XmlSerializerOptions.Strictand setMaxDocumentByteCount. See Security. - Dispose what
SerializeToDocumentreturns. TheXmlDocumentholds pooled memory. - Catch
XmlExceptionaroundDeserialize. 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 aUtf8XmlWriter, write the parts that aren’t objects, and let the serializer write each object as one element.
Deserialize the interesting elements of a large document
Walk the document with aUtf8XmlReader 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.
Query first, then map
Parse withXmlDocument, find an element, and deserialize that element.
Considerations
Keep the following in mind when you choose overloads and when you compareXmlSerializer with the serializers you already know.
Reflection and trimming
The overloads that takeXmlSerializerOptions 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
Serializeto astringor abyte[], andSerializeAsyncto aStream, build the whole document in a pooled buffer first.SerializeAsyncthen writes it to the stream in one asynchronous write.DeserializeandDeserializeAsyncfrom aStreamor aPipeReaderread to the end into a pooled buffer before parsing.MaxDocumentByteCountlimits how much is read.Deserializefrom anXmlElementor anXmlNodewrites that element to a pooled buffer and reads it back.- To process a large document without holding it all in memory, use
SerializeAsyncEnumerableandDeserializeAsyncEnumerable. 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 anXmlElement.RespectRequiredConstructorParametersdefaults totrue, so a document that lacks a value for a constructor parameter with no default value is rejected. See Immutable types.- A
nullmember is written as an element markedxsi:nil="true"by default, because XML has nonullliteral. - 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.Serializationare read. Attributes fromSystem.Xml.Serialization,IXmlSerializable, and data contract attributes are ignored. - Collections have no wrapper element unless you add
[XmlArray]or setDefaultCollectionMappingtoWrapped.XmlSerializerOptions.Bclsets 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.
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 istrue 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 oneArrayOf* 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
TheArrayOf* 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
Related APIs
- 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