Skip to main content

Definition

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

Syntax

Summary

Specifies how a Utf8XmlReader handles whitespace-only text between elements.

Usage

Set XmlReaderOptions.WhitespaceHandling to choose what a Utf8XmlReader does with text that is only white space and sits between elements. That’s the indentation of a formatted document. White-space-only text that is content is reported under every setting: the whole content of an element (````csharp ````), text after other text or a CDATA section in the same element (mixed content), and anything inside an element where xml:space="preserve" is in force. With the default, the document below produces three Text tokens for the indentation inside Order. With Insignificant, they’re gone:
View the complete sample on GitHub

Remarks

Whitespace inside a leaf element’s value is always significant. <n> a </n> is the value " a " regardless of this setting. XmlWhitespaceHandling.Significant is the zero value of the enumeration, so it is the default wherever a whitespace handling value is not set: in XmlReaderOptions and XmlDocumentOptions, whether they are created with new or as default. Whitespace between elements, such as indentation, is kept by default so that XML that is read and written back does not change its layout. XmlSerializerOptions.WhitespaceHandling defaults to XmlWhitespaceHandling.Insignificant, because indentation has no place in an object.

Considerations

  • Significant is the zero value, and the default. new XmlReaderOptions(), default(XmlReaderOptions), and a reader created without options all report white space between elements. It’s kept by default so that XML that’s read and written back doesn’t change its layout.
  • Set Insignificant in a loop that only wants data. Otherwise the token after a start tag that has child elements is usually a Text token that holds indentation.
  • The serializer’s default is different. XmlSerializerOptions.WhitespaceHandling defaults to Insignificant, because indentation has no place in an object.
  • The setting never changes a value. White space inside an element’s text is part of the value under every setting: <Name> Ada </Name> is " Ada ". The reader doesn’t trim. Line ends are read as a line feed under every setting, as XML requires.
  • Only indentation is skipped. Under Insignificant and None, white-space-only text is still a Text token when it’s the whole content of its element, when its element already has text or a CDATA section before it, as in the space between </b> and * in <p>a **b** <i>c*</p>, or when xml:space="preserve" applies. xml:space="preserve" holds for the element and everything inside it, until an element sets xml:space="default".
  • Reading in blocks doesn’t change the result. XmlReaderState carries xml:space and whether each open element has text, so a document split across blocks reports the same white space as one read whole.
  • White space outside the root element is never reported. The line break after the XML declaration and anything after the root’s end tag are skipped under every setting.
  • None rejects indented documents. Use it only when the producer is known to write no indentation and anything else is an error. It accepts the white space that is content, as listed above.
  • XmlDocument uses the same enumeration. XmlDocumentOptions.WhitespaceHandling also defaults to Significant.

Values

  • CloudNimble.Text.Xml.XmlReaderOptions
  • CloudNimble.Text.Xml.XmlDocumentOptions
  • CloudNimble.Text.Xml.XmlTokenType
  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.XmlCommentHandling