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

# XmlExtensionData

> Represents the XML attributes and child elements that have no matching member and are captured by a member marked with [XmlExtensionDataAttribute](/xmlessentials/api-refer...

## Definition

**Assembly:** CloudNimble.XmlEssentials.dll

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

## Syntax

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

## Summary

Represents the XML attributes and child elements that have no matching member and are captured by a member marked with
[XmlExtensionDataAttribute](https://easyaf.nimbleapps.cloud/api-reference/CloudNimble/Text/Xml/Serialization/XmlExtensionDataAttribute). This class cannot be inherited.

## Usage

`XmlExtensionData` holds the attributes and child elements of an element that no member of your type maps. Add a member of this type, mark it with `[XmlExtensionData]`, and the serializer fills it when it reads and writes its content back when it serializes. It is the XML counterpart of the dictionary that `[JsonExtensionData]` uses in System.Text.Json.

```csharp theme={"dark"}
public sealed class Settings
{
    public string? Theme { get; set; }

    [XmlExtensionData]
    public XmlExtensionData? Extra { get; set; }
}
```

| Property | Type | Holds |
| - | - | - |
| `Attributes` | `Dictionary<XmlName, string>` | Each unmapped attribute, keyed by its qualified name. The value is the attribute's text with references resolved. |
| `Elements` | `List<XmlElementNode>` | Each unmapped child element, in document order, with its attributes and content. |

Both collections are created with the object and are never `null`. You can read them after deserializing, and add to them before serializing.

## Examples

### Round-trip content the type doesn't declare

The `Settings` type maps only `Theme`. The document also has two attributes and two elements, one of each in a namespace. They are captured, and written back after the mapped member changes.

```csharp theme={"dark"}
string xml = """
    <Settings version="2" xmlns:ui="urn:example:ui" ui:density="compact">
      <Theme>Dark</Theme>
      <Telemetry enabled="false" />
      <ui:Font size="14">Cascadia Code</ui:Font>
    </Settings>
    """;

Settings? settings = XmlSerializer.Deserialize<Settings>(xml);
output.WriteLine($"Theme: {settings!.Theme}");

foreach (KeyValuePair<XmlName, string> attribute in settings.Extra!.Attributes)
{
    output.WriteLine($"Attribute: {attribute.Key} = {attribute.Value}");
}

foreach (XmlElementNode element in settings.Extra.Elements)
{
    output.WriteLine($"Element: {element.LocalName}");
}

settings.Theme = "Light";
var options = new XmlSerializerOptions { WriteIndented = true, NewLine = "\n", OmitXmlDeclaration = true };
output.WriteLine(XmlSerializer.Serialize(settings, options));
```

The code produces the following output:

```text theme={"dark"}
Theme: Dark
Attribute: version = 2
Attribute: ui:density = compact
Element: Telemetry
Element: Font
<Settings version="2" xmlns:ui="urn:example:ui" ui:density="compact">
  <Theme>Light</Theme>
  <Telemetry enabled="false"/>
  <ui:Font size="14">Cascadia Code</ui:Font>
</Settings>
```

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

## Considerations

* **Position.** When writing, unmapped attributes follow the mapped attributes and unmapped elements follow the mapped elements. Where an unmapped element stood among the mapped ones is not recorded.
* **Namespace declarations.** `xmlns` attributes are not captured and are not written from `Attributes`. The writer declares the namespaces that the captured names need.
* **Prefixes.** A captured attribute keeps the prefix it was read with, in the `Prefix` of its `XmlName`. An attribute that you add with a namespace and no prefix gets a generated prefix.
* **Serializer attributes.** `xsi:nil`, `xsi:type`, and the `id` and `ref` attributes of reference handling are consumed by the serializer and are not captured.
* **Keys.** `XmlName` equality ignores the prefix, so two attributes with the same local name and namespace are one entry.
* **Allocation.** Each captured element is a mutable `XmlElementNode` tree. Capturing large unmapped subtrees costs one node for every element, attribute, and text run in them. If you don't need the content, leave the member out and let the serializer skip it.
* **Thread safety.** The object is an ordinary mutable class with a dictionary and a list. Don't change it from several threads at once.

## Constructors

### &#x20;.ctor

#### Syntax

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

### &#x20;.ctor Inherited

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

#### Syntax

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

## Properties

### &#x20;Attributes

Gets the XML attributes that have no matching member.

#### Syntax

```csharp theme={"dark"}
public System.Collections.Generic.Dictionary<CloudNimble.Text.Xml.XmlName, string> Attributes { get; }
```

#### Property Value

Type: `System.Collections.Generic.Dictionary<CloudNimble.Text.Xml.XmlName, string>`
A dictionary that maps the name of each unmapped attribute to its value.

### &#x20;Elements

Gets the child elements that have no matching member.

#### Syntax

```csharp theme={"dark"}
public System.Collections.Generic.List<CloudNimble.Text.Xml.Nodes.XmlElementNode> Elements { get; }
```

#### Property Value

Type: `System.Collections.Generic.List<CloudNimble.Text.Xml.Nodes.XmlElementNode>`
A list of the unmapped child elements, in document order.

## Methods

### &#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;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;ToString Inherited Virtual

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

#### Syntax

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

#### Returns

Type: `string?`

## Related APIs

* CloudNimble.Text.Xml.Serialization.XmlExtensionDataAttribute
* CloudNimble.Text.Xml.Serialization.XmlUnmappedMemberHandling
* CloudNimble.Text.Xml.XmlName
* CloudNimble.Text.Xml.Nodes.XmlElementNode
* CloudNimble.Text.Xml.XmlSerializerOptions


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