Skip to main content

Definition

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

Syntax

Summary

Specifies how a Utf8XmlWriter writes an element that has no content.

Usage

Set XmlWriterOptions.EmptyElementStyle, or XmlSerializerOptions.EmptyElementStyle, to choose how an element with no content is written. The sample writes one document three times. The first two use SelfClosingWithSpace and the third uses StartAndEndTag:
View the complete sample on GitHub

Remarks

The three forms mean the same thing to a reader. The setting makes the output consistent: every element with no content is written in the one form, whichever code wrote it. XmlEmptyElementStyle.SelfClosing is the zero value, so it is the default in XmlWriterOptions, created with new or as default, and in XmlSerializerOptions. Utf8XmlWriter.WriteFullEndElement always writes a start tag and an end tag, whatever the setting is. An element that was given an empty string has content: a value of no characters. It is written as <e></e> whatever the setting is, as the serializer writes a string that is empty, which keeps it apart from an element that was given nothing and from one that is nil.

Considerations

  • SelfClosing is the zero value, and the default. new XmlWriterOptions(), default(XmlWriterOptions), a writer created without options, and new XmlSerializerOptions() all write <e/>.
  • The three forms mean the same thing to a reader. The setting changes how the output looks, not what it holds.
  • It is one rule for all output. Your own calls to the writer, the serializer, a generated context, XmlDocument.WriteTo and XmlNode.WriteTo write an element with no content the same way.
  • It applies when an element is ended with nothing in it. That is WriteEndElement before any content is written, the WriteEmptyElement methods, and WriteElementString with a null string. An element with attributes and no content counts: <e a="1" />.
  • WriteFullEndElement always writes an end tag, whatever the setting is.
  • An empty string is content. WriteElementString("e", ""), and a string member that holds "", are written as <e></e> under every value. That keeps an empty string apart from an element given nothing and from one marked xsi:nil.
  • A document or a node keeps an end tag it read. An element that was parsed as <e></e> is written as <e></e> under every value. An element that was parsed as <e/> or <e /> takes the form the setting asks for. For a node, XmlElementNode.IsEmptyElement holds which of the two it is.
  • Indentation puts nothing between the two tags of <e></e>, because white space there would be content.

Values

  • CloudNimble.Text.Xml.XmlWriterOptions
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlAttributeStyle
  • CloudNimble.Text.Xml.Nodes.XmlElementNode