> ## Documentation Index
> Fetch the complete documentation index at: https://easyaf.nimbleapps.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# XmlSourceGenerationMode

> Specifies the generation mode for the XmlEssentials source generator.

## Definition

**Assembly:** CloudNimble.XmlEssentials.dll

**Namespace:** CloudNimble.Text.Xml.Serialization

**Inheritance:** System.Enum

## Syntax

```csharp theme={"dark"}
CloudNimble.Text.Xml.Serialization.XmlSourceGenerationMode
```

## Summary

Specifies the generation mode for the XmlEssentials source generator.

## Usage

Set the mode with the `GenerationMode` property of `[XmlSourceGenerationOptions]` on a context. It applies to every type of the context. To give one listed type another mode, set `GenerationMode` on its `[XmlSerializable<T>]` attribute.

```csharp theme={"dark"}
[XmlSourceGenerationOptions(GenerationMode = XmlSourceGenerationMode.Metadata)]
[XmlSerializable<Order>]
public partial class MetadataOrderXmlContext : XmlSerializerContext
{
}
```

| Member | What the generator writes for each object type | Behavior |
| - | - | - |
| `Default` | Metadata and a write fast path. | The fast path writes when the options allow it. The serializer does everything else from the metadata. |
| `Metadata` | Metadata only. | The serializer reads and writes from the metadata. |
| `Serialization` | The write fast path only. | The type info writes with the options the context was compiled for. Every read, and a write with other options, throws `InvalidOperationException`. `XmlTypeInfo<T>.Properties` is empty. |

Combining `Metadata` and `Serialization` gives the same result as `Default`.

The metadata describes the constructor and the members of a type: `XmlObjectInfoValues<T>`, `XmlPropertyInfoValues<TValue>`, and `XmlParameterInfoValues`. The serializer reads and writes from it with the engine the reflection resolver uses, so every option, converter, and attribute has the same effect. The write fast path is a method that writes the type directly. It's used when the options agree with what `[XmlSourceGenerationOptions]` declares. Every mode is safe for trimming and Native AOT.

The following example writes and reads the same order with a context generated in each mode:

```csharp theme={"dark"}
XmlTypeInfo<Order> both = OrderXmlContext.Default.Order;
XmlTypeInfo<Order> metadata = MetadataOrderXmlContext.Default.Order;
XmlTypeInfo<Order> serialization = SerializationOrderXmlContext.Default.Order;

var order = new Order { Id = 7, Customer = "Grace" };
string xml = XmlSerializer.Serialize(order, both);
output.WriteLine($"Default:       {xml}");
output.WriteLine($"Metadata:      {XmlSerializer.Serialize(order, metadata)}");
output.WriteLine($"Serialization: {XmlSerializer.Serialize(order, serialization)}");

output.WriteLine($"Default:       {both.Properties.Count} properties, reads {XmlSerializer.Deserialize(xml, both)!.Customer}");
output.WriteLine($"Metadata:      {metadata.Properties.Count} properties, reads {XmlSerializer.Deserialize(xml, metadata)!.Customer}");
try
{
    XmlSerializer.Deserialize(xml, serialization);
}
catch (InvalidOperationException ex)
{
    output.WriteLine($"Serialization: {serialization.Properties.Count} properties, {ex.Message}");
}
```

The code produces the following output:

```text theme={"dark"}
Default:       <?xml version="1.0" encoding="utf-8"?><Order Id="7"><Customer>Grace</Customer></Order>
Metadata:      <?xml version="1.0" encoding="utf-8"?><Order Id="7"><Customer>Grace</Customer></Order>
Serialization: <?xml version="1.0" encoding="utf-8"?><Order Id="7"><Customer>Grace</Customer></Order>
Default:       3 properties, reads Grace
Metadata:      3 properties, reads Grace
Serialization: 0 properties, The type info for 'CloudNimble.XmlEssentials.Samples.Models.Advanced.Order' was generated in XmlSourceGenerationMode.Serialization, which provides no property metadata. It can only write a type that has a write fast path, with the options it was compiled for. Use XmlSourceGenerationMode.Default or XmlSourceGenerationMode.Metadata.
```

The names and the modes match `JsonSourceGenerationMode` in System.Text.Json. For a comparison of the modes, see [Source-generation modes](https://easyaf.nimbleapps.cloud/xmlessentials/guides/source-generation-modes.md).

[View the complete sample on GitHub](https://github.com/CloudNimble/XmlEssentials/blob/main/src/CloudNimble.XmlEssentials.Samples/33_SourceGenerationModes.cs)

## Remarks

Type metadata describes the constructor and the members of a type. The serializer reads and writes the type from
it with the rules it applies to a type it maps through reflection, and with no reflection over the members.
Serialization logic is a write fast path: a method that writes the type directly, which is used when the options
agree with the [XmlSourceGenerationOptionsAttribute](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlSourceGenerationOptionsAttribute.md) of the context. Every mode is safe for trimming
and Native AOT. This enumeration is analogous to `JsonSourceGenerationMode` in `System.Text.Json`.

## Values

| Name | Value | Description |
| - | - | - |
| `Default` | 0 | The default mode, in which both type metadata and serialization logic are generated. The result is the same as |
| combining [XmlSourceGenerationMode.Metadata](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlSourceGenerationMode.md) and [XmlSourceGenerationMode.Serialization](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlSourceGenerationMode.md). | | |
| The write fast path writes when the options allow it, and the serializer does everything else from the metadata. | | |
| `Metadata` | 1 | Type-metadata initialization logic only. The serializer reads and writes the types of the context from the metadata. |
| `Serialization` | 2 | Serialization logic only: a write fast path and no type metadata. A type can be written with the options the |
| context was compiled for. Every read, and a write with other options, throws [InvalidOperationException](https://learn.microsoft.com/dotnet/api/system.invalidoperationexception), | | |
| and [XmlTypeInfo.Properties](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlTypeInfo.md#properties) is empty. | | |

## Related APIs

* CloudNimble.Text.Xml.Serialization.XmlSourceGenerationOptionsAttribute
* CloudNimble.Text.Xml.Serialization.XmlSerializableAttribute
* CloudNimble.Text.Xml.Serialization.XmlSerializerContext
* CloudNimble.Text.Xml.Serialization.DefaultXmlTypeInfoResolver


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.