Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueTypeSyntax
Summary
Provides the ability for the user to define custom behavior when parsing XML to create an XmlDocument.Usage
Pass anXmlDocumentOptions 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
CommentHandlingtoXmlCommentHandling.Skipto leave comments out.Disallowrejects a document that contains one. - Set
WhitespaceHandlingtoXmlWhitespaceHandling.Insignificantto leave out text that is only white space between elements.Nonerejects a document that contains it.
CountChildren is a helper in the sample that counts the nodes EnumerateChildren returns. The code produces the following output:
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 anXmlException whose message names the limit.
Leave out white space between elements
By default the indentation of a document is kept asText nodes. Insignificant leaves it out.
Best Practices
- Keep the defaults when the document is written back.
CommentHandling.AllowandWhitespaceHandling.Significantare what keep comments and indentation through a round trip. - Set
SkipandInsignificantwhen 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
MaxDocumentByteCountfor input you don’t control. It’s the only limit with no default, and with theStreamoverload ofParseit stops the read as soon as the stream is found to be longer. - Lower
MaxDepthwhen you know the shape. The default of 64 is generous for most formats. - Use
DisalloworNoneto enforce a contract, for example a message format that must not carry comments.
Considerations
XmlDocumentOptionsis a struct.default(XmlDocumentOptions)andnew XmlDocumentOptions()are the same options:CommentHandlingisAllowandWhitespaceHandlingisSignificantin both.XmlReaderOptionshas the same two defaults, so aUtf8XmlReaderreports comments and white-space-only text as well.XmlSerializerskips both. A document keeps them so thatXmlDocument.WriteToreproduces the comments and the layout that were parsed.- Because white space is kept,
XmlElement.EnumerateChildrenon an indented document returns aTextnode 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. WhitespaceHandlingapplies 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. MaxDocumentByteCountcounts UTF-8 bytes. For thestringandReadOnlySpan<char>overloads ofParse, it applies to the size after conversion to UTF-8.XmlDocument.ParseValuedoesn’t take these options. TheXmlReaderOptionsof the reader you pass apply.XmlNode.Parsetakes no options. It keeps comments and white space and uses the default limits. To build a mutable tree with other options, parse withXmlDocumentand callXmlElement.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.ValueTypeSyntax
.ctor Inherited
Inherited from
objectSyntax
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.ValueTypeSyntax
Parameters
Returns
Type:bool
Equals Inherited Virtual
Inherited from
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Override
Inherited from
System.ValueTypeSyntax
Returns
Type:int
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetType Inherited
Inherited from
objectSyntax
Returns
Type:System.Type
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
ToString Override
Inherited from
System.ValueTypeSyntax
Returns
Type:string?
ToString Inherited Virtual
Inherited from
objectSyntax
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