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

# XmlDocumentOptions

> Provides the ability for the user to define custom behavior when parsing XML to create an [XmlDocument](/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocument).

## Definition

**Assembly:** CloudNimble.XmlEssentials.dll

**Namespace:** CloudNimble.Text.Xml

**Inheritance:** System.ValueType

## Syntax

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

## Summary

Provides the ability for the user to define custom behavior when parsing XML to create an
[XmlDocument](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocument.md).

## Usage

Pass an `XmlDocumentOptions` to `XmlDocument.Parse`, `ParseAsync`, `TryParse`, or `DiscoverNamespaces` to choose what the parser keeps and what it rejects. `default`, `new XmlDocumentOptions()`, and a call to `Parse` without options all mean the same options.

The defaults are chosen so that there are no surprises on a round trip: a document that is parsed and written back with `XmlDocument.WriteTo` keeps its comments and the white space between its elements. When you only read values and don't want either in the document:

* Set `CommentHandling` to `XmlCommentHandling.Skip` to leave comments out. `Disallow` rejects a document that contains one.
* Set `WhitespaceHandling` to `XmlWhitespaceHandling.Insignificant` to leave out text that is only white space between elements. `None` rejects a document that contains it.

```csharp theme={"dark"}
const string xml = "<Order><!-- rush --><Item>Keyboard</Item></Order>";

using (XmlDocument kept = XmlDocument.Parse(xml))
{
    output.WriteLine($"Allow (the default): {CountChildren(kept.RootElement)} children");
    foreach (XmlElement child in kept.RootElement.EnumerateChildren())
    {
        output.WriteLine($"  {child.ValueKind}: '{child.GetString()}'");
    }
}

var skip = new XmlDocumentOptions { CommentHandling = XmlCommentHandling.Skip };
using (XmlDocument skipped = XmlDocument.Parse(xml, skip))
{
    output.WriteLine($"Skip: {CountChildren(skipped.RootElement)} child");
}

var disallow = new XmlDocumentOptions { CommentHandling = XmlCommentHandling.Disallow };
try
{
    using XmlDocument rejected = XmlDocument.Parse(xml, disallow);
}
catch (XmlException ex)
{
    output.WriteLine($"Disallow: {ex.Message}");
}
```

`CountChildren` is a helper in the sample that counts the nodes `EnumerateChildren` returns. The code produces the following output:

```text theme={"dark"}
Allow (the default): 2 children
  Comment: ' rush '
  Element: 'Keyboard'
Skip: 1 child
Disallow: Comments are not allowed.
```

The options are:

| Property | Controls | Default |
| - | - | - |
| `CommentHandling` | Whether comments are kept, dropped, or rejected | `Allow` (kept) |
| `WhitespaceHandling` | What happens to white space that is the only text between two tags | `Significant` (kept) |
| `MaxDepth` | How deeply elements nest | 0, which means 64 |
| `MaxAttributeCount` | Attributes on one element, namespace declarations included | 0, which means 1,000 |
| `MaxNameByteCount` | One element or attribute name, in UTF-8 bytes | 0, which means 1,024 |
| `MaxTextByteCount` | One text node, CDATA section, attribute value, or processing instruction's data, in UTF-8 bytes | 0, which means 16,777,216 |
| `MaxCommentByteCount` | One comment, in UTF-8 bytes | 0, which means 1,048,576 |
| `MaxDocumentByteCount` | The whole document, in UTF-8 bytes | 0, which means no limit |

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

## Remarks

The defaults are chosen so that a document that is parsed and then written with
[XmlDocument.WriteTo](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocument.md#writeto) keeps its comments and the white space between its
elements. If you don't want comments in the document, set [XmlDocumentOptions.CommentHandling](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md#commenthandling) to
[XmlCommentHandling.Skip](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlCommentHandling.md). If you don't want white-space-only text in the document, set
[XmlDocumentOptions.WhitespaceHandling](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md#whitespacehandling) to [XmlWhitespaceHandling.Insignificant](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlWhitespaceHandling.md).
`default` and `new XmlDocumentOptions()` are the same options.
A limit property that is set to 0 uses its default limit, except
[XmlDocumentOptions.MaxDocumentByteCount](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md#maxdocumentbytecount), where 0 means that the size is not limited.

## Examples

### Limit untrusted input

A limit that is exceeded throws an `XmlException` whose message names the limit.

```csharp theme={"dark"}
var options = new XmlDocumentOptions
{
    MaxDepth = 2,
    MaxDocumentByteCount = 64,
};

try
{
    using XmlDocument tooDeep = XmlDocument.Parse("<a>**<c/>**</a>", options);
}
catch (XmlException ex)
{
    output.WriteLine(ex.Message);
}

try
{
    using XmlDocument tooLarge = XmlDocument.Parse($"<a>{new string('x', 100)}</a>", options);
}
catch (XmlException ex)
{
    output.WriteLine(ex.Message);
}
```

The code produces the following output:

```text theme={"dark"}
MaxDepth exceeded (2).
MaxDocumentByteCount exceeded (64).
```

### Leave out white space between elements

By default the indentation of a document is kept as `Text` nodes. `Insignificant` leaves it out.

```csharp theme={"dark"}
const string xml = """
    <Order>
      <Item>Keyboard</Item>
      <Item>Mouse</Item>
    </Order>
    """;

using (XmlDocument document = XmlDocument.Parse(xml))
{
    XmlElement order = document.RootElement;
    output.WriteLine($"Significant (the default): {CountChildren(order)} children");
    foreach (XmlElement child in order.EnumerateChildren())
    {
        output.WriteLine($"  {child.ValueKind}");
    }

    output.WriteLine($"  Text of the root: '{order.GetString()!.Replace("\n", "\\n")}'");
    output.WriteLine($"  Text of the first Item: '{order.GetChild("Item").GetString()}'");
}

var options = new XmlDocumentOptions { WhitespaceHandling = XmlWhitespaceHandling.Insignificant };
using (XmlDocument document = XmlDocument.Parse(xml, options))
{
    XmlElement order = document.RootElement;
    output.WriteLine($"Insignificant: {CountChildren(order)} children");
    output.WriteLine($"  Text of the root: '{order.GetString()}'");
}
```

The code produces the following output:

```text theme={"dark"}
Significant (the default): 5 children
  Text
  Element
  Text
  Element
  Text
  Text of the root: '\n  Keyboard\n  Mouse\n'
  Text of the first Item: 'Keyboard'
Insignificant: 2 children
  Text of the root: 'KeyboardMouse'
```

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

## Best Practices

* **Keep the defaults when the document is written back.** `CommentHandling.Allow` and `WhitespaceHandling.Significant` are what keep comments and indentation through a round trip.
* **Set `Skip` and `Insignificant` when you only read values.** The children of an element are then only the nodes that carry data, and the text of an element doesn't include indentation.
* **Set `MaxDocumentByteCount` for input you don't control.** It's the only limit with no default, and with the `Stream` overload of `Parse` it stops the read as soon as the stream is found to be longer.
* **Lower `MaxDepth` when you know the shape.** The default of 64 is generous for most formats.
* **Use `Disallow` or `None` to enforce a contract,** for example a message format that must not carry comments.

## Considerations

* `XmlDocumentOptions` is a struct. `default(XmlDocumentOptions)` and `new XmlDocumentOptions()` are the same options: `CommentHandling` is `Allow` and `WhitespaceHandling` is `Significant` in both.
* `XmlReaderOptions` has the same two defaults, so a `Utf8XmlReader` reports comments and white-space-only text as well. `XmlSerializer` skips both. A document keeps them so that `XmlDocument.WriteTo` reproduces the comments and the layout that were parsed.
* Because white space is kept, `XmlElement.EnumerateChildren` on an indented document returns a `Text` node for each run of indentation, and the text of an element that has child elements includes it. Lookups by name and the values of leaf elements aren't affected.
* `WhitespaceHandling` applies only to text that is nothing but white space between two tags. White space inside a value, such as `<Item> Keyboard </Item>`, is always kept.
* White space outside the root element isn't stored under any setting.
* A limit of 0 means the default limit in the table under Usage, except `MaxDocumentByteCount`, where 0 means no limit.
* `MaxDocumentByteCount` counts UTF-8 bytes. For the `string` and `ReadOnlySpan<char>` overloads of `Parse`, it applies to the size after conversion to UTF-8.
* `XmlDocument.ParseValue` doesn't take these options. The `XmlReaderOptions` of the reader you pass apply.
* `XmlNode.Parse` takes no options. It keeps comments and white space and uses the default limits. To build a mutable tree with other options, parse with `XmlDocument` and call `XmlElement.CloneNode`.

## Constructors

### &#x20;.ctor

Initializes a new instance of the [XmlDocumentOptions](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md) struct.

#### Syntax

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

#### Remarks

[XmlDocumentOptions.CommentHandling](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md#commenthandling) is [XmlCommentHandling.Allow](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlCommentHandling.md),
[XmlDocumentOptions.WhitespaceHandling](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlDocumentOptions.md#whitespacehandling) is [XmlWhitespaceHandling.Significant](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlWhitespaceHandling.md), and every
limit property is set to 0, which selects its default limit.

### &#x20;.ctor Inherited

> [!NOTE]
>
> Inherited from `System.ValueType`

#### Syntax

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

### &#x20;.ctor Inherited

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

#### Syntax

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

## Properties

### &#x20;CommentHandling

Gets or sets a value that determines how the parser handles comments.

#### Syntax

```csharp theme={"dark"}
public CloudNimble.Text.Xml.XmlCommentHandling CommentHandling { get; set; }
```

#### Property Value

Type: `CloudNimble.Text.Xml.XmlCommentHandling`
One of the enumeration values that indicates how comments are handled. The default is
[XmlCommentHandling.Allow](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlCommentHandling.md).

#### Remarks

Comments are kept by default so that parsing a document and writing it back does not lose them.
Set this property to [XmlCommentHandling.Skip](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlCommentHandling.md) to leave comments out of the document,
or to [XmlCommentHandling.Disallow](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlCommentHandling.md) to reject a document that contains one.

### &#x20;MaxAttributeCount

Gets or sets the maximum number of attributes allowed on one element.

#### Syntax

```csharp theme={"dark"}
public int MaxAttributeCount { get; set; }
```

#### Property Value

Type: `int`
The maximum number of attributes on one element, including namespace declarations
(`xmlns` and `xmlns:prefix`). The default is 0, which means 1000.

### &#x20;MaxCommentByteCount

Gets or sets the maximum size allowed for the content of one comment.

#### Syntax

```csharp theme={"dark"}
public int MaxCommentByteCount { get; set; }
```

#### Property Value

Type: `int`
The maximum size of the content of a comment, in UTF-8 bytes. The default is 0, which means 1,048,576.

### &#x20;MaxDepth

Gets or sets the maximum depth allowed when parsing XML.

#### Syntax

```csharp theme={"dark"}
public int MaxDepth { get; set; }
```

#### Property Value

Type: `int`
The maximum depth of nested elements. The default is 0, which means 64.

### &#x20;MaxDocumentByteCount

Gets or sets the maximum size allowed for the document.

#### Syntax

```csharp theme={"dark"}
public int MaxDocumentByteCount { get; set; }
```

#### Property Value

Type: `int`
The maximum size of the document, in UTF-8 bytes. The default is 0, which means that the size is not limited.

### &#x20;MaxNameByteCount

Gets or sets the maximum size allowed for an element name or an attribute name.

#### Syntax

```csharp theme={"dark"}
public int MaxNameByteCount { get; set; }
```

#### Property Value

Type: `int`
The maximum size of an element or attribute name, in UTF-8 bytes. The default is 0, which means 1024.

### &#x20;MaxTextByteCount

Gets or sets the maximum size allowed for a text node, a CDATA section, an attribute value, or the data of a processing instruction.

#### Syntax

```csharp theme={"dark"}
public int MaxTextByteCount { get; set; }
```

#### Property Value

Type: `int`
The maximum size of each, in UTF-8 bytes as they appear in the document. The default is 0, which means
16,777,216.

### &#x20;WhitespaceHandling

Gets or sets a value that determines how the parser handles text between elements that consists only of white space.

#### Syntax

```csharp theme={"dark"}
public CloudNimble.Text.Xml.XmlWhitespaceHandling WhitespaceHandling { get; set; }
```

#### Property Value

Type: `CloudNimble.Text.Xml.XmlWhitespaceHandling`
One of the enumeration values that indicates how white-space-only text between elements is handled.
The default is [XmlWhitespaceHandling.Significant](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlWhitespaceHandling.md).

#### Remarks

White space between elements, such as indentation, is kept by default so that parsing a document
and writing it back does not change its layout. Each run of white space is a text node, so it
appears when you enumerate an element's children. Set this property to
[XmlWhitespaceHandling.Insignificant](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlWhitespaceHandling.md) to leave white-space-only text out of the
document, or to [XmlWhitespaceHandling.None](https://easyaf.nimbleapps.cloud/xmlessentials/api-reference/CloudNimble/Text/Xml/XmlWhitespaceHandling.md) to reject a document that contains it.

## Methods

### &#x20;Equals Override

> [!NOTE]
>
> Inherited from `System.ValueType`

#### Syntax

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

#### Parameters

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

#### Returns

Type: `bool`

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

> [!NOTE]
>
> Inherited from `System.ValueType`

#### Syntax

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

#### Returns

Type: `int`

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

> [!NOTE]
>
> Inherited from `System.ValueType`

#### Syntax

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

#### Returns

Type: `string?`

### &#x20;ToString Inherited Virtual

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

#### Syntax

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

#### Returns

Type: `string?`

## Related APIs

* CloudNimble.Text.Xml.XmlDocument
* CloudNimble.Text.Xml.XmlCommentHandling
* CloudNimble.Text.Xml.XmlWhitespaceHandling
* CloudNimble.Text.Xml.XmlReaderOptions
* CloudNimble.Text.Xml.XmlException


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