Skip to main content

Definition

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

Syntax

Summary

Provides the options that control how a Utf8XmlWriter formats and checks the XML it writes.

Usage

Create the options, set the properties you need, and pass the value to a Utf8XmlWriter constructor, as in new Utf8XmlWriter(buffer, options). A writer keeps its options for its whole life, including across Reset.
The properties fall into four groups: With no options at all, the writer produces one line of XML that starts with an XML 1.0 declaration, with validation on.

Remarks

A limit property (XmlWriterOptions.MaxAttributeCount, XmlWriterOptions.MaxDepth, XmlWriterOptions.MaxNameByteCount) that is 0 uses the default limit that the property documents. new XmlWriterOptions() sets XmlWriterOptions.IndentCharacter to a space and XmlWriterOptions.NewLine to NewLine. default(XmlWriterOptions) leaves them unset, and the writer then uses the same two values.

Examples

Each example is a sample in the repository whose output is checked by a test.

Indentation

WriteParagraph writes the same small document with the options it’s given.
View the complete sample on GitHub

The XML declaration

WriteOrder writes one empty element with the options it’s given.
View the complete sample on GitHub

Best Practices

  • Set NewLine when the bytes matter. The default is Environment.NewLine, which differs between Windows and Linux. Choose "\n" or "\r\n" when the output is compared, hashed, signed, or checked in. The value is used for every line break, including the ones inside text, so it matters for output that isn’t indented too.
  • Set EmptyElementStyle and AttributeStyle to match a file you edit. A document or a node that is written back then differs from its source only where you changed it. SelfClosingWithSpace gives the <e /> form that the writers in System.Xml use.
  • Don’t indent output that only programs read. Indentation makes the output larger and changes nothing a parser cares about.
  • Use OmitXmlDeclaration for fragments that go inside another document or message.
  • Leave SkipValidation off until a measurement shows that you need it, and then only for code whose output you test. With it on, the writer can produce XML that no reader accepts.
  • Leave EmitByteOrderMark off unless a consumer asks for the byte order mark. UTF-8 doesn’t need one.
  • Keep Version at XML 1.0 unless your data holds control characters that only XML 1.1 can carry.

Considerations

Know the following about the defaults and what each group of options does.

Defaults

new and default behave the same

XmlWriterOptions is a struct. default(XmlWriterOptions) leaves IndentCharacter as the null character and NewLine as null, and the writer treats both as their documented defaults. That’s why the options parameter of the Utf8XmlWriter constructors can be omitted.

Limits

A limit of 0 selects its default. There’s no value that turns a limit off, but SkipValidation skips the MaxNameByteCount check along with the name checks. MaxAttributeCount includes namespace declarations, including the ones the writer adds. MaxNameByteCount applies to element names, attribute names, and prefixes. The writer has no limit on text or on the size of the document.

What indentation does

Indented adds a line break and indentation before elements, comments, and processing instructions. An element that holds text, a CDATA section, or raw XML is written on one line, with any child elements, so that no value changes. IndentCharacter accepts a space or a tab; any other character throws ArgumentOutOfRangeException.

What the layout options do

  • NewLine is written for every line break: the ones Indented and XmlAttributeStyle.PerLine add, and the ones inside text, CDATA sections, comments, and processing instructions. It accepts "\n" and "\r\n"; any other string throws ArgumentOutOfRangeException. No value changes, because a reader gives a carriage return followed by a line feed back as one line feed. A carriage return that is part of a value is written as &#xD;, and a line break in an attribute value as &#xA;.
  • EmptyElementStyle sets the form of an element with no content: <e/>, <e />, or <e></e>. It applies to WriteEndElement on an element with no content, to WriteEmptyElement, and to WriteElementString with a null string. WriteFullEndElement always writes an end tag.
  • AttributeStyle set to PerLine puts AttributesPerLine attributes on each line, with or without Indented. Each line after the first is indented so that its first attribute is under the first attribute of the tag. Namespace declarations and xsi:nil count as attributes.

What SkipValidation skips

With SkipValidation set, the writer doesn’t check names, namespace declarations, duplicate attributes, the characters of comments, CDATA sections, and processing instructions, or that every element is closed when the writer is disposed. It also doesn’t check WriteRawValue input. It still escapes values and rejects characters XML doesn’t allow in them, allows only one root element, rejects text and CDATA sections at the document level, rejects -- in a comment, ]]> in a CDATA section, and ?> in processing instruction data, and enforces MaxDepth and MaxAttributeCount.

Constructors

.ctor

Initializes a new instance of the XmlWriterOptions struct with a space as the indent character, an indent size of 0 (which means 2), and NewLine as the line ending.

Syntax

.ctor Inherited

Inherited from System.ValueType

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

AttributesPerLine

Gets or sets the number of attributes the writer writes to a line when XmlWriterOptions.AttributeStyle is XmlAttributeStyle.PerLine.

Syntax

Property Value

Type: int The number of attributes to a line. The default is 0, which means 1. The value is not read when XmlWriterOptions.AttributeStyle is XmlAttributeStyle.Inline.

Exceptions

AttributeStyle

Gets or sets a value that specifies how the writer lays out the attributes of a start tag.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlAttributeStyle One of the enumeration values. The default is XmlAttributeStyle.Inline, which writes every attribute on the line of the start tag.

Remarks

With XmlAttributeStyle.PerLine, XmlWriterOptions.AttributesPerLine attributes are written to a line and each line after the first is indented to put its first attribute under the first attribute of the tag. The indentation is the white space that starts the line of the tag, and then one space for each character of the tag up to its first attribute. It applies with or without XmlWriterOptions.Indented.

EmitByteOrderMark

Gets or sets a value that indicates whether the writer writes a UTF-8 byte order mark before its first output.

Syntax

Property Value

Type: bool true if the writer writes a UTF-8 byte order mark; otherwise, false. The default is false.

EmptyElementStyle

Gets or sets a value that specifies how the writer writes an element that has no content.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlEmptyElementStyle One of the enumeration values. The default is XmlEmptyElementStyle.SelfClosing, which writes <e/>.

Remarks

The setting applies to an element ended with Utf8XmlWriter.WriteEndElement before any content was written, and to the WriteEmptyElement methods. Utf8XmlWriter.WriteFullEndElement always writes an end tag.

IndentCharacter

Gets or sets the character used to indent when XmlWriterOptions.Indented is true.

Syntax

Property Value

Type: char The indent character. The default is a space. The null character ('\0'), which is the value in default(XmlWriterOptions), also means a space.

Indented

Gets or sets a value that indicates whether the writer formats the output with new lines and indentation.

Syntax

Property Value

Type: bool true if the writer puts each element on its own line and indents it; otherwise, false. The default is false.

IndentSize

Gets or sets the number of XmlWriterOptions.IndentCharacter characters written for each level of depth when XmlWriterOptions.Indented is true.

Syntax

Property Value

Type: int The number of indent characters for each level. The default is 0, which means 2.

MaxAttributeCount

Gets or sets the maximum number of attributes on one element, including namespace declarations.

Syntax

Property Value

Type: int The maximum number of attributes on one element. The default is 0, which means 1000.

MaxDepth

Gets or sets the maximum depth of nested elements.

Syntax

Property Value

Type: int The maximum element depth. The default is 0, which means 64.

MaxNameByteCount

Gets or sets the maximum length of an element name, an attribute name, or a prefix, in UTF-8 bytes.

Syntax

Property Value

Type: int The maximum name length, in UTF-8 bytes. The default is 0, which means 1024.

NewLine

Gets or sets the line ending the writer writes.

Syntax

Property Value

Type: string The line ending: "\n" or "\r\n". The default is NewLine, in an instance created with new and in default alike.

Exceptions

Remarks

The value is written for every line break: the ones XmlWriterOptions.Indented and XmlAttributeStyle.PerLine add, and the ones in text, CDATA sections, comments and processing instructions. So a document has one kind of line ending, whatever it was read with. No value changes. A reader gives a carriage return and a line feed back as a line feed (XML 1.0 and 1.1, section 2.11). A carriage return that is part of a value is written as a character reference, as it always is, and a line break in an attribute value is written as a character reference too.

OmitXmlDeclaration

Gets or sets a value that indicates whether the writer omits the XML declaration (<?xml ...?>).

Syntax

Property Value

Type: bool true if the writer does not write the XML declaration on its own; otherwise, false. The default is false.

Remarks

Utf8XmlWriter.WriteXmlDeclaration writes the declaration whatever this value is.

SkipValidation

Gets or sets a value that indicates whether the writer skips its name, namespace, and duplicate attribute checks.

Syntax

Property Value

Type: bool true if the writer skips those checks; otherwise, false. The default is false.

Remarks

When the value is true, the writer does not check that names are valid XML names or fit in XmlWriterOptions.MaxNameByteCount, that prefixes and namespace declarations follow Namespaces in XML, that an element has no duplicate attribute, that the characters of a comment, CDATA section, or processing instruction are allowed, or that every element is closed when the writer is disposed. The writer still enforces a single root element, XmlWriterOptions.MaxDepth, and XmlWriterOptions.MaxAttributeCount.

Standalone

Gets or sets the value of the standalone pseudo-attribute of the XML declaration.

Syntax

Property Value

Type: bool? true to write standalone="yes"; false to write standalone="no"; null to omit the pseudo-attribute. The default is null.

Version

Gets or sets the XML version that the writer writes in the XML declaration and uses to decide which characters are allowed.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlVersion One of the enumeration values that specifies the XML version. The default is XmlVersion.Version10.

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.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlVersion
  • CloudNimble.Text.Xml.XmlReaderOptions
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlException