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

# XmlDeclarationNode

> Represents a mutable XML declaration. 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.XmlDeclarationNode
```

## Summary

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

## Usage

`XmlDeclarationNode` is the XML declaration, `<?xml version="1.0" encoding="utf-8"?>`, as a node you can set up and write. `Version` defaults to `1.0`, `Encoding` to `utf-8`, and `Standalone` to `null`, which leaves `standalone` out.

You seldom need it. A `Utf8XmlWriter` writes a declaration by itself unless `XmlWriterOptions.OmitXmlDeclaration` is `true`, as the first half of this example shows. Use the node when you want to state the declaration yourself, for example to write `standalone="yes"`. Write it first, to a writer that omits its own declaration.

```csharp theme={"dark"}
var order = new XmlElementNode("Order");
order.SetAttribute("id", "1042");
order.AddElement("Item").InnerText = "Keyboard";

var buffer = new ArrayBufferWriter<byte>();
var options = new XmlWriterOptions { Indented = true, NewLine = "\n" };
using (var writer = new Utf8XmlWriter(buffer, options))
{
    order.WriteTo(writer);
}

output.WriteLine(Encoding.UTF8.GetString(buffer.WrittenSpan));

buffer.ResetWrittenCount();
options = new XmlWriterOptions { OmitXmlDeclaration = true };
using (var writer = new Utf8XmlWriter(buffer, options))
{
    new XmlDeclarationNode { Version = "1.0", Standalone = true }.WriteTo(writer);
    order.WriteTo(writer);
}

output.WriteLine(Encoding.UTF8.GetString(buffer.WrittenSpan));
```

The code produces the following output:

```xml theme={"dark"}
<?xml version="1.0" encoding="utf-8"?>
<Order id="1042">
  <Item>Keyboard</Item>
</Order>
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Order id="1042"><Item>Keyboard</Item></Order>
```

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

## Remarks

The read-only [XmlDocument](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocument.md) reports its declaration as an [XmlDeclarationInfo](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDeclarationInfo.md) instead.
[XmlDeclarationNode.WriteTo](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlDeclarationNode.md#writeto) writes [XmlDeclarationNode.Version](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlDeclarationNode.md#version) and [XmlDeclarationNode.Encoding](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/Nodes/XmlDeclarationNode.md#encoding) as they
are, without validating them.

## Considerations

* `WriteTo` writes the declaration and records it with the writer, so the writer doesn't add a second one, whatever `OmitXmlDeclaration` is set to.
* The node must be written before anything else. It can't be added to an element's `Children`; that throws an `XmlException`.
* `Version` and `Encoding` are written as they are, without validation. XmlEssentials writes UTF-8 only, so leave `Encoding` at `utf-8`. Setting `Version` to `1.1` changes the declaration, not the writer; set `XmlWriterOptions.Version` as well.
* `XmlNode.Parse` never returns this node, and a tree has no document node to hold it. The declaration of a parsed document is `XmlDocument.Declaration`, an `XmlDeclarationInfo`.
* This is the only node type whose `ValueKind` is `XmlDeclaration`.

## Constructors

### &#x20;.ctor

#### Syntax

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

### &#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;Encoding

Gets or sets the encoding name that the declaration states.

#### Syntax

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

#### Property Value

Type: `string`
The value of the `encoding` pseudo-attribute. The default is `utf-8`.

### &#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;Standalone

Gets or sets the standalone document declaration.

#### Syntax

```csharp theme={"dark"}
public bool? Standalone { get; set; }
```

#### Property Value

Type: `bool?`
[`true`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) to write `standalone="yes"`; [`false`](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) to write
`standalone="no"`; [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) to omit the `standalone` pseudo-attribute.
The default is [`null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

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

### &#x20;Version

Gets or sets the XML version that the declaration states.

#### Syntax

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

#### Property Value

Type: `string`
The value of the `version` pseudo-attribute. The default is `1.0`.

## 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.XmlDeclarationInfo
* CloudNimble.Text.Xml.XmlWriterOptions
* CloudNimble.Text.Xml.Utf8XmlWriter
* CloudNimble.Text.Xml.XmlDocument
* CloudNimble.Text.Xml.Nodes.XmlNode


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