> ## 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.

# XmlConverterFactory

> Supports converting several types by using a factory pattern.

## Definition

**Assembly:** CloudNimble.XmlEssentials.dll

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

**Inheritance:** CloudNimble.Text.Xml.Serialization.XmlConverter

## Syntax

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

## Summary

Supports converting several types by using a factory pattern.

## Usage

A factory is a converter that doesn't convert anything itself. It tells the serializer which types it covers, in `CanConvert`, and creates the converter for each one, in `CreateConverter`. Use it for a family of types that one `XmlConverter<T>` can't serve: every enumeration, or every closed form of a generic type.

```csharp theme={"dark"}
public sealed class KebabCaseEnumConverterFactory : XmlConverterFactory
{
    public override bool CanConvert(Type typeToConvert)
    {
        return typeToConvert.IsEnum;
    }

    public override XmlConverter? CreateConverter(Type typeToConvert, XmlSerializerOptions options)
    {
        Type converterType = typeof(KebabCaseEnumConverter<>).MakeGenericType(typeToConvert);
        return (XmlConverter?)Activator.CreateInstance(converterType);
    }
}
```

Register a factory as you would a converter: add an instance to `XmlSerializerOptions.Converters`, or name the factory type in `[XmlConverter]`.

`CreateConverter` must return an `XmlConverter<T>` whose `T` is exactly `typeToConvert`. The class is the counterpart of `JsonConverterFactory` in System.Text.Json.

## Remarks

Use a factory for a family of types, such as every enumeration or every closed form of a generic type.

## Examples

### Write every enumeration in kebab case

The factory in the usage section creates one of these converters for each enumeration:

```csharp theme={"dark"}
public sealed class KebabCaseEnumConverter<TEnum> : XmlConverter<TEnum>
    where TEnum : struct, Enum
{
    public override TEnum Read(ref Utf8XmlReader reader, Type typeToConvert, XmlSerializerOptions options)
    {
        TEnum value = default;
        if (reader.IsEmptyElement)
        {
            return value;
        }

        while (reader.Read() && reader.TokenType is not XmlTokenType.EndElement)
        {
            if (reader.TokenType is XmlTokenType.Text)
            {
                value = Parse(reader.GetString()!);
            }
        }

        return value;
    }

    public override void Write(Utf8XmlWriter writer, TEnum value, XmlSerializerOptions options)
    {
        writer.WriteStringValue(XmlNamingPolicy.KebabCaseLower.ConvertName(value.ToString()));
    }

    private static TEnum Parse(string text)
    {
        foreach (TEnum candidate in Enum.GetValues<TEnum>())
        {
            if (XmlNamingPolicy.KebabCaseLower.ConvertName(candidate.ToString()) == text)
            {
                return candidate;
            }
        }

        throw new XmlException($"'{text}' is not a value of {typeof(TEnum).Name}.");
    }
}
```

With the factory registered, both enumeration members of `OrderTracking` use it:

```csharp theme={"dark"}
var options = new XmlSerializerOptions { OmitXmlDeclaration = true };
options.Converters.Add(new KebabCaseEnumConverterFactory());

var tracking = new OrderTracking
{
    OrderId = 1042,
    Status = OrderStatus.ReadyToShip,
    Priority = Priority.Rush,
};

string xml = XmlSerializer.Serialize(tracking, options);
output.WriteLine(xml);

OrderTracking? copy = XmlSerializer.Deserialize<OrderTracking>(xml, options);
output.WriteLine($"Order {copy!.OrderId}: {copy.Status}, {copy.Priority}");
```

The code produces the following output:

```text theme={"dark"}
<OrderTracking><OrderId>1042</OrderId><Priority>rush</Priority><Status>ready-to-ship</Status></OrderTracking>
Order 1042: ReadyToShip, Rush
```

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

## Considerations

* **The returned converter must match the type exactly.** If `CreateConverter` returns `null`, the serializer throws `InvalidOperationException`. It also throws when the converter returned is for another type.
* **`CreateConverter` runs once for each type and options instance.** The serializer keeps the converter it returns, so the work of creating it isn't repeated.
* **Order in the `Converters` list matters.** The first converter or factory whose `CanConvert` returns `true` is used. Put a converter for one specific type before a factory that also covers it.
* **Reflection in a factory isn't safe for Native AOT.** A factory that calls `MakeGenericType` or `Activator.CreateInstance` on a constructed type needs code the ahead-of-time compiler may not have produced. In an AOT application, construct the converters directly, for example in a `switch` on `typeToConvert`.
* **Source generation.** A source-generated context in the default mode or in `Metadata` mode uses a factory as the reflection resolver does. A factory in `XmlSerializerOptions.Converters` applies to every type it covers: enumerations, classes, and structs.
* **A factory makes `CanConvert` broad, so keep it precise.** A factory that returns `true` for types it can't handle hides the built-in converter for those types.

## Constructors

### &#x20;.ctor Inherited

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

Limits derivation to `XmlConverter`1\` and [XmlConverterFactory](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlConverterFactory.md).

#### Syntax

```csharp theme={"dark"}
private protected XmlConverter()
```

### &#x20;.ctor Inherited

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public Object()
```

## Properties

### &#x20;CanPopulate Inherited Virtual

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

Gets whether the converter can read an element into a value that already exists.

#### Syntax

```csharp theme={"dark"}
internal virtual bool CanPopulate { get; }
```

#### Property Value

Type: `bool`
[`true`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) for the converters of objects and dictionaries that the library creates; otherwise,
[`false`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). A converter the caller wrote creates its value, so a member it reads is replaced.

### &#x20;IsValueConverter Inherited Virtual

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

Gets whether this is one of the built-in converters that read and write a value from its lexical form,
which the serializer can call directly.

#### Syntax

```csharp theme={"dark"}
internal virtual bool IsValueConverter { get; }
```

#### Property Value

Type: `bool`

## Methods

### &#x20;CanConvert Inherited Abstract

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

When overridden in a derived class, determines whether the converter instance can convert the specified type.

#### Syntax

```csharp theme={"dark"}
public abstract bool CanConvert(System.Type typeToConvert)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `typeToConvert` | `System.Type` | The type to check. |

#### Returns

Type: `bool`
[`true`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) if the instance can convert *typeToConvert*; otherwise, [`false`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool).

### &#x20;CreateConverter Abstract

When overridden in a derived class, creates a converter for the specified type.

#### Syntax

```csharp theme={"dark"}
public abstract CloudNimble.Text.Xml.Serialization.XmlConverter CreateConverter(System.Type typeToConvert, CloudNimble.Text.Xml.XmlSerializerOptions options)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `typeToConvert` | `System.Type` | The type handled by the converter. |
| `options` | `CloudNimble.Text.Xml.XmlSerializerOptions` | The serialization options to use. |

#### Returns

Type: `CloudNimble.Text.Xml.Serialization.XmlConverter?`
A converter that derives from `XmlConverter`1` for exactly *typeToConvert*. The serializer throws an [InvalidOperationException](https://learn.microsoft.com/dotnet/api/system.invalidoperationexception) if the factory returns [`null\`]\([https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null)).

### &#x20;CreateTypeInfo Inherited Abstract

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

Creates type info that reads and writes *typeToConvert* with this converter.

#### Syntax

```csharp theme={"dark"}
internal abstract CloudNimble.Text.Xml.Serialization.XmlTypeInfo CreateTypeInfo(System.Type typeToConvert, CloudNimble.Text.Xml.XmlSerializerOptions options)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `typeToConvert` | `System.Type` | The CLR type. |
| `options` | `CloudNimble.Text.Xml.XmlSerializerOptions` | The serializer options. |

#### Returns

Type: `CloudNimble.Text.Xml.Serialization.XmlTypeInfo?`
The type info, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) when this converter cannot produce values of that exact type.

### &#x20;Equals Inherited Virtual

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public virtual bool Equals(object obj)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `obj` | `object?` | - |

#### Returns

Type: `bool`

### &#x20;Equals Inherited

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public static bool Equals(object objA, object objB)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `objA` | `object?` | - |
| `objB` | `object?` | - |

#### Returns

Type: `bool`

### &#x20;GetHashCode Inherited Virtual

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public virtual int GetHashCode()
```

#### Returns

Type: `int`

### &#x20;GetType Inherited

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public System.Type GetType()
```

#### Returns

Type: `System.Type`

### &#x20;MemberwiseClone Inherited

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
protected internal object MemberwiseClone()
```

#### Returns

Type: `object`

### &#x20;Populate Inherited Virtual

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

Reads the element *reader* is on into *existing*.

#### Syntax

```csharp theme={"dark"}
internal virtual void Populate(ref CloudNimble.Text.Xml.Utf8XmlReader reader, object existing, CloudNimble.Text.Xml.XmlSerializerOptions options)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `reader` | `CloudNimble.Text.Xml.Utf8XmlReader` | The reader, on a start tag that is not marked nil. |
| `existing` | `object` | The value to populate. A value type is boxed, and the box is what is changed. |
| `options` | `CloudNimble.Text.Xml.XmlSerializerOptions` | The serializer options. |

#### Exceptions

| Exception | Description |
| - | - |
| `NotSupportedException` | The converter cannot populate: [XmlConverter.CanPopulate](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlConverterFactory.md#canpopulate) is [`false`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). |

### &#x20;ReferenceEquals Inherited

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public static bool ReferenceEquals(object objA, object objB)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `objA` | `object?` | - |
| `objB` | `object?` | - |

#### Returns

Type: `bool`

### &#x20;Resolve Inherited Abstract

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Serialization.XmlConverter`

Returns the converter that does the work for *typeToConvert*: this converter, or the
one a factory creates.

#### Syntax

```csharp theme={"dark"}
internal abstract CloudNimble.Text.Xml.Serialization.XmlConverter Resolve(System.Type typeToConvert, CloudNimble.Text.Xml.XmlSerializerOptions options)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `typeToConvert` | `System.Type` | The CLR type. |
| `options` | `CloudNimble.Text.Xml.XmlSerializerOptions` | The serializer options. |

#### Returns

Type: `CloudNimble.Text.Xml.Serialization.XmlConverter`
The converter.

### &#x20;ToString Inherited Virtual

> [!NOTE]
>
> Inherited from `object`

#### Syntax

```csharp theme={"dark"}
public virtual string ToString()
```

#### Returns

Type: `string?`

## Related APIs

* CloudNimble.Text.Xml.Serialization.XmlConverter
* CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
* CloudNimble.Text.Xml.XmlSerializerOptions
* CloudNimble.Text.Xml.XmlNamingPolicy


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