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

# XmlNamedValue

> Represents a value together with the name of the element it is written under. This class cannot be inherited.

## Definition

**Assembly:** CloudNimble.XmlEssentials.dll

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

## Syntax

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

## Summary

Represents a value together with the name of the element it is written under. This class cannot be inherited.

## Usage

An `XmlNamedValue` is a value together with the name of the element it is written under. Use it for a member that has a choice of elements, set with several `[XmlElement<T>]` attributes, when two of the elements hold the same type. The type of the value can't say which element it belongs to, so the value carries the element's name instead.

```csharp theme={"dark"}
public sealed class ContactCard
{
    [XmlElement<string>("Home")]
    [XmlElement<string>("Work")]
    public List<XmlNamedValue> Phones { get; set; } = [];
}
```

```csharp theme={"dark"}
var options = new XmlSerializerOptions { OmitXmlDeclaration = true };
var card = new ContactCard
{
    Phones = { new XmlNamedValue("Work", "555-0100"), new XmlNamedValue("Home", "555-0199") },
};

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

ContactCard read = XmlSerializer.Deserialize<ContactCard>(xml, options)!;
foreach (XmlNamedValue phone in read.Phones)
{
    output.WriteLine($"{phone.Name}: {phone.Value}");
}
```

The code produces the following output:

```text theme={"dark"}
<ContactCard><Work>555-0100</Work><Home>555-0199</Home></ContactCard>
Work: 555-0100
Home: 555-0199
```

Declare the member as `XmlNamedValue` for one value, or as a collection of it for a sequence. `Name` must be the name of one of the member's elements, and `Value` must be of the type that element holds; anything else throws a `NotSupportedException` when it is written. A `Value` that is `null` is written as an element marked `xsi:nil="true"`. Set `Namespace` only when two of the member's elements have one local name in two namespaces. A value that was read has the namespace its element was declared with.

See [When two elements hold the same type](https://easyaf.nimbleapps.cloud/xmlessentials/guides/polymorphism.md#when-two-elements-hold-the-same-type) and the [polymorphism sample](https://github.com/CloudNimble/XmlEssentials/blob/main/src/CloudNimble.XmlEssentials.Samples/16_Polymorphism.cs).

## Remarks

A member that carries more than one `XmlElementAttribute`1\` holds a choice of elements, and a
value is written under the element of its type. When two of the elements hold the same type, the type does
not say which element a value is. Declare the member as [XmlNamedValue](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlNamedValue.md), or as a collection of
it, and each value says which element it is by name.

[XmlNamedValue.Name](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlNamedValue.md#name) must be the name of one of the member's elements, and [XmlNamedValue.Value](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlNamedValue.md#value) must be of
the type that element holds. A value that is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) is written as an element marked
`xsi:nil`.

## Examples

```csharp theme={"dark"}
public sealed class Contact
{
    [XmlElement<string>("Home")]
    [XmlElement<string>("Work")]
    public List<XmlNamedValue> Phones { get; set; } = [];
}

var contact = new Contact { Phones = { new XmlNamedValue("Work", "555-0100"), new XmlNamedValue("Home", "555-0199") } };
string xml = XmlSerializer.Serialize(contact);
// <Contact><Work>555-0100</Work><Home>555-0199</Home></Contact>
```

## Constructors

### &#x20;.ctor

Initializes a new instance of the [XmlNamedValue](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlNamedValue.md) class with no name and no value.

#### Syntax

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

### &#x20;.ctor

Initializes a new instance of the [XmlNamedValue](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Serialization/XmlNamedValue.md) class with the specified element name and value.

#### Syntax

```csharp theme={"dark"}
public XmlNamedValue(string name, object value)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `name` | `string` | The local name of the element. |
| `value` | `object?` | The value of the element. |

#### Exceptions

| Exception | Description |
| - | - |
| `ArgumentException` | *name* is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null), empty, or white space. |

### &#x20;.ctor Inherited

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

#### Syntax

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

## Properties

### &#x20;Name

Gets or sets the local name of the element.

#### Syntax

```csharp theme={"dark"}
public string Name { get; set; }
```

#### Property Value

Type: `string`
The local name of the element the value is written under. The default is an empty string.

### &#x20;Namespace

Gets or sets the namespace URI of the element.

#### Syntax

```csharp theme={"dark"}
public string Namespace { get; set; }
```

#### Property Value

Type: `string?`
The namespace URI the member gives the element, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) to find the element by its
local name alone. The default is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

#### Remarks

Set it when two elements of the member have one local name in two namespaces. A value that was read
has the namespace its element was declared with.

### &#x20;Value

Gets or sets the value of the element.

#### Syntax

```csharp theme={"dark"}
public object Value { get; set; }
```

#### Property Value

Type: `object?`
The value, of the type the element holds, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). The default is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

## 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?`


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