Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueType

Syntax

Summary

Provides the ability for the user to define custom behavior when parsing XML to create an XmlDocument.

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.
CountChildren is a helper in the sample that counts the nodes EnumerateChildren returns. The code produces the following output:
The options are: View the complete sample on GitHub

Remarks

The defaults are chosen so that a document that is parsed and then written with XmlDocument.WriteTo keeps its comments and the white space between its elements. If you don’t want comments in the document, set XmlDocumentOptions.CommentHandling to XmlCommentHandling.Skip. If you don’t want white-space-only text in the document, set XmlDocumentOptions.WhitespaceHandling to XmlWhitespaceHandling.Insignificant. default and new XmlDocumentOptions() are the same options. A limit property that is set to 0 uses its default limit, except XmlDocumentOptions.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.
The code produces the following output:

Leave out white space between elements

By default the indentation of a document is kept as Text nodes. Insignificant leaves it out.
The code produces the following output:
View the complete sample on GitHub

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

.ctor

Initializes a new instance of the XmlDocumentOptions struct.

Syntax

Remarks

XmlDocumentOptions.CommentHandling is XmlCommentHandling.Allow, XmlDocumentOptions.WhitespaceHandling is XmlWhitespaceHandling.Significant, and every limit property is set to 0, which selects its default limit.

.ctor Inherited

Inherited from System.ValueType

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

CommentHandling

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

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlCommentHandling One of the enumeration values that indicates how comments are handled. The default is XmlCommentHandling.Allow.

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 to leave comments out of the document, or to XmlCommentHandling.Disallow to reject a document that contains one.

MaxAttributeCount

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

Syntax

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.

MaxCommentByteCount

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

Syntax

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.

MaxDepth

Gets or sets the maximum depth allowed when parsing XML.

Syntax

Property Value

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

MaxDocumentByteCount

Gets or sets the maximum size allowed for the document.

Syntax

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.

MaxNameByteCount

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

Syntax

Property Value

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

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

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.

WhitespaceHandling

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

Syntax

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.

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 to leave white-space-only text out of the document, or to XmlWhitespaceHandling.None to reject a document that contains it.

Methods

Equals Override

Inherited from System.ValueType

Syntax

Parameters

Returns

Type: bool

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetHashCode Override

Inherited from System.ValueType

Syntax

Returns

Type: int

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Override

Inherited from System.ValueType

Syntax

Returns

Type: string?

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlCommentHandling
  • CloudNimble.Text.Xml.XmlWhitespaceHandling
  • CloudNimble.Text.Xml.XmlReaderOptions
  • CloudNimble.Text.Xml.XmlException