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

# XmlAttributeNode

> Represents a mutable XML attribute. This class cannot be inherited.

## Definition

**Assembly:** CloudNimble.XmlEssentials.dll

**Namespace:** CloudNimble.Text.Xml.Nodes

**Inheritance:** CloudNimble.Text.Xml.Nodes.XmlNode

## Syntax

```csharp theme={"dark"}
CloudNimble.Text.Xml.Nodes.XmlAttributeNode
```

## Summary

Represents a mutable XML attribute. This class cannot be inherited.

## Usage

`XmlAttributeNode` is an attribute in the mutable tree: a `LocalName`, a `Value`, and, for an attribute in a namespace, a `NamespaceUri` and a `Prefix`. Attributes live in the `Attributes` list of an `XmlElementNode`.

Most code doesn't create one directly. `XmlElementNode.SetAttribute` creates or updates an attribute, and `Attributes["name"]` returns the node so that you can read or change its `Value`. Construct an `XmlAttributeNode` yourself when you need to choose the prefix.

```csharp theme={"dark"}
var order = (XmlElementNode)XmlNode.Parse("""<Order id="1042" status="open" draft="true"/>""")!;

order.SetAttribute("status", "shipped");
order.SetAttribute("carrier", "Contoso Freight");
order.Attributes["id"]!.Value = "1043";

XmlAttributeNode draft = order.Attributes["draft"]!;
order.Attributes.Remove(draft);

output.WriteLine(order.ToString());
```

The code produces the following output:

```xml theme={"dark"}
<Order id="1043" status="shipped" carrier="Contoso Freight"/>
```

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

## Considerations

* An attribute without a prefix is in no namespace, even when its element is in a default namespace. To put an attribute in a namespace, give it a `NamespaceUri` and a `Prefix`. With a namespace and no prefix, the writer generates one:

  ```csharp theme={"dark"}
  var item = new XmlElementNode("Item");
  item.SetAttribute("unit", "kg", "urn:example:units");
  output.WriteLine(item.ToString());

  var broken = new XmlElementNode("Item", namespaceUri: null, prefix: "u");
  try
  {
      broken.ToString();
  }
  catch (XmlException ex)
  {
      output.WriteLine(ex.Message);
  }
  ```

  The code produces the following output:

  ```text theme={"dark"}
  <Item xmlns:ns1="urn:example:units" ns1:unit="kg"/>
  Prefixed names require a namespace URI.
  ```

* A namespace declaration is an attribute too. In a parsed tree, `xmlns="..."` has the local name `xmlns`, and `xmlns:dc="..."` has the prefix `xmlns` and the local name `dc`; both report `XmlNamespaces.Xmlns` as their namespace.

* `Value` is the plain value. Escaping happens when the attribute is written. A `null` value, prefix, or namespace is stored as an empty string.

* `LocalName` rejects `null`, an empty string, and white space.

* `ToString` on an attribute throws an `XmlException`, because an attribute can't be written outside a start tag. Read `Value`.

* An attribute has one parent. Adding it to a second element throws; use `DeepClone` to copy it.

## Constructors

### &#x20;.ctor

Initializes a new instance of the [XmlAttributeNode](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlAttributeNode.md) class with the specified local name, value, namespace URI, and prefix.

#### Syntax

```csharp theme={"dark"}
public XmlAttributeNode(string localName, string value = null, string namespaceUri = null, string prefix = null)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `localName` | `string` | The local name of the attribute, without a prefix. |
| `value` | `string?` | The value of the attribute. The default is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null), which is stored as an empty string. |
| `namespaceUri` | `string?` | The namespace URI of the attribute, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) or an empty string for an attribute in no namespace. The default is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |
| `prefix` | `string?` | The prefix of the attribute name, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) or an empty string for no prefix. The default is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |

#### Exceptions

| Exception | Description |
| - | - |
| `ArgumentNullException` | *localName* is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |
| `ArgumentException` | *localName* is empty or consists only of white-space characters. |

### &#x20;.ctor Inherited

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

#### Syntax

```csharp theme={"dark"}
protected XmlNode()
```

### &#x20;.ctor Inherited

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

#### Syntax

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

## Properties

### &#x20;LocalName

Gets or sets the local name of this attribute.

#### Syntax

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

#### Property Value

Type: `string`
The local name of the attribute, without a prefix.

#### Exceptions

| Exception | Description |
| - | - |
| `ArgumentNullException` | The value in a set operation is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |
| `ArgumentException` | The value in a set operation is empty or consists only of white-space characters. |

### &#x20;NamespaceUri Override

Gets or sets the namespace URI of this attribute.

#### Syntax

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

#### Property Value

Type: `string?`
The namespace URI of the attribute, or an empty string if the attribute is in no namespace. The
value is never [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). Setting [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) stores an empty string.

### &#x20;NamespaceUri Inherited Virtual

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Gets or sets the namespace URI of this node.

#### Syntax

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

#### Property Value

Type: `string?`
The namespace URI of the node. An element or an attribute in no namespace gives an empty string.
For a node that is not an element or an attribute, the value is always [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null),
and setting it has no effect.

### &#x20;Parent Inherited

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Gets the parent of this node.

#### Syntax

```csharp theme={"dark"}
public CloudNimble.Text.Xml.Nodes.XmlNode Parent { get; }
```

#### Property Value

Type: `CloudNimble.Text.Xml.Nodes.XmlNode?`
The element that holds this node as a child or as an attribute, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) if this
node has no parent.

### &#x20;Prefix

Gets or sets the namespace prefix of this attribute.

#### Syntax

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

#### Property Value

Type: `string`
The prefix of the attribute name, or an empty string if the name has no prefix. Setting
[`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) stores an empty string.

### &#x20;Value

Gets or sets the value of this attribute.

#### Syntax

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

#### Property Value

Type: `string`
The value of the attribute, without escaping. Setting [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) stores an empty string.

### &#x20;ValueKind Override

#### Syntax

```csharp theme={"dark"}
public override CloudNimble.Text.Xml.XmlValueKind ValueKind { get; }
```

#### Property Value

Type: `CloudNimble.Text.Xml.XmlValueKind`

### &#x20;ValueKind Inherited Abstract

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

When overridden in a derived class, gets the kind of this node.

#### Syntax

```csharp theme={"dark"}
public abstract CloudNimble.Text.Xml.XmlValueKind ValueKind { get; }
```

#### Property Value

Type: `CloudNimble.Text.Xml.XmlValueKind`
One of the enumeration values that indicates the kind of this node.

## Methods

### &#x20;DeepClone Override

#### Syntax

```csharp theme={"dark"}
public override CloudNimble.Text.Xml.Nodes.XmlNode DeepClone()
```

#### Returns

Type: `CloudNimble.Text.Xml.Nodes.XmlNode`

### &#x20;DeepClone Inherited Abstract

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

When overridden in a derived class, creates a new instance of the node. All children and
attributes are recursively cloned.

#### Syntax

```csharp theme={"dark"}
public abstract CloudNimble.Text.Xml.Nodes.XmlNode DeepClone()
```

#### Returns

Type: `CloudNimble.Text.Xml.Nodes.XmlNode`
A new cloned instance of the current node. The clone has no parent.

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

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Parses a string that holds a single XML document into a mutable tree.

#### Syntax

```csharp theme={"dark"}
public static CloudNimble.Text.Xml.Nodes.XmlNode Parse(string xml)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `xml` | `string` | The XML text to parse. |

#### Returns

Type: `CloudNimble.Text.Xml.Nodes.XmlNode`
An [XmlNode](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlNode.md) representation of the document element.

#### Exceptions

| Exception | Description |
| - | - |
| `ArgumentNullException` | *xml* is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |
| `XmlException` | *xml* is not a well-formed XML document. |
| -or- | |
| *xml* exceeds a default limit of [XmlDocumentOptions](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md). | |

#### Remarks

Comments inside the document element are kept. The XML declaration and any comments or processing
instructions outside the document element are not part of the returned tree.

### &#x20;Parse Inherited

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Parses a span of UTF-8 bytes that holds a single XML document into a mutable tree.

#### Syntax

```csharp theme={"dark"}
public static CloudNimble.Text.Xml.Nodes.XmlNode Parse(System.ReadOnlySpan<byte> xml)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `xml` | `System.ReadOnlySpan<byte>` | The UTF-8 encoded XML text to parse. |

#### Returns

Type: `CloudNimble.Text.Xml.Nodes.XmlNode`
An [XmlNode](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlNode.md) representation of the document element.

#### Exceptions

| Exception | Description |
| - | - |
| `XmlException` | *xml* is not a well-formed XML document. |
| -or- | |
| *xml* exceeds a default limit of [XmlDocumentOptions](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md). | |

#### Remarks

Comments inside the document element are kept. The XML declaration and any comments or processing
instructions outside the document element are not part of the returned tree.

### &#x20;ParseValue Inherited

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Parses one XML element (including its attributes and descendants) from the provided reader into a mutable tree.

#### Syntax

```csharp theme={"dark"}
public static CloudNimble.Text.Xml.Nodes.XmlNode ParseValue(ref CloudNimble.Text.Xml.Utf8XmlReader reader)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `reader` | `CloudNimble.Text.Xml.Utf8XmlReader` | The reader to read. It is positioned on or before the start tag of the element. |

#### Returns

Type: `CloudNimble.Text.Xml.Nodes.XmlNode`
An [XmlNode](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlNode.md) representation of the element (and nested nodes) read from the reader.

#### Exceptions

| Exception | Description |
| - | - |
| `XmlException` | *reader* reaches an end tag before it reaches a start tag. |
| -or- | |
| *reader* reaches the end of its input without reading an element. | |
| -or- | |
| The XML that *reader* reads is not well formed. | |

#### Remarks

When this method returns, the reader is positioned on the end tag of the element, or on the
element itself if it is an empty-element tag. Comments are kept only if the reader reports them.

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

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Sets the parent pointer.

#### Syntax

```csharp theme={"dark"}
internal void SetParent(CloudNimble.Text.Xml.Nodes.XmlNode parent)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `parent` | `CloudNimble.Text.Xml.Nodes.XmlNode?` | The parent, or [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |

### &#x20;ToString Override

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

Converts the current instance to a string that holds its XML markup.

#### Syntax

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

#### Returns

Type: `string`
The XML markup of this node, including its attributes and descendants. No XML declaration is added.

#### Exceptions

| Exception | Description |
| - | - |
| `XmlException` | The tree cannot be written as XML, for example because a name has a prefix but no namespace. |

### &#x20;ToString Inherited Virtual

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

#### Syntax

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

#### Returns

Type: `string?`

### &#x20;WriteTo Override

#### Syntax

```csharp theme={"dark"}
public override void WriteTo(CloudNimble.Text.Xml.Utf8XmlWriter writer)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `writer` | `CloudNimble.Text.Xml.Utf8XmlWriter` | - |

### &#x20;WriteTo Inherited Abstract

> [!NOTE]
>
> Inherited from `CloudNimble.Text.Xml.Nodes.XmlNode`

When overridden in a derived class, writes this node to the provided writer as XML.

#### Syntax

```csharp theme={"dark"}
public abstract void WriteTo(CloudNimble.Text.Xml.Utf8XmlWriter writer)
```

#### Parameters

| Name | Type | Description |
| - | - | - |
| `writer` | `CloudNimble.Text.Xml.Utf8XmlWriter` | The writer to which to write the node. |

#### Exceptions

| Exception | Description |
| - | - |
| `ArgumentNullException` | *writer* is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). |

## Related APIs

* CloudNimble.Text.Xml.Nodes.XmlAttributeNodeList
* CloudNimble.Text.Xml.Nodes.XmlElementNode
* CloudNimble.Text.Xml.Nodes.XmlNode
* CloudNimble.Text.Xml.XmlNamespaces
* CloudNimble.Text.Xml.XmlAttributes


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