Skip to main content

Summary

The CloudNimble.Text.Xml.Nodes namespace contains the mutable XML tree: XmlNode and the types that derive from it for elements, attributes, text, CDATA sections, comments, processing instructions, and the XML declaration, together with the two list types that hold an element’s children and attributes. It’s the XML counterpart of System.Text.Json.Nodes. Use it to build XML node by node or to change XML that you parsed. To read XML without changing it, use XmlDocument in the CloudNimble.Text.Xml namespace.

Usage

Create an XmlElementNode and add to it, or parse existing XML with XmlNode.Parse. ToString returns the markup.
The code produces the following output:
For a walkthrough, see How to build and change XML with XmlNode. View the complete sample on GitHub

Examples

Parse a document and read from the tree

The code produces the following output:
Four of the seven child nodes are the white space between the elements, which the tree keeps.

Add each kind of node

The code produces the following output:
View sample 29 and sample 30 on GitHub.

Best Practices

  • Use the tree to change or build XML, and XmlDocument to read it. The tree has an object for every node it makes and a string for every name and value in it. The document doesn’t.
  • Reach into only what you edit. A tree from XmlNode.Parse makes the nodes under an element when you first reach into it, through Children, Attributes, InnerText, an indexer, or a change. An element you never reach into is written from the parsed input.
  • Clone the element you edit when you parse with XmlDocument. XmlElement.CloneNode copies everything under the element at once, so clone the smallest element that changes.
  • Find elements by name or type, not by position. A tree parsed from an indented document has a text node for each run of indentation, so Children[0] is often white space.
  • Use SetAttribute to add or change an attribute. It updates an attribute that exists. Attributes.Add doesn’t check for a duplicate, and a duplicate fails when the tree is written.
  • Give every namespaced element its namespace. A child doesn’t inherit the namespace of its parent.
  • Copy with DeepClone to use a node in two places. A node has one parent, and adding a node that already has one throws.
  • Write to a Utf8XmlWriter for anything but a quick look. ToString builds a string with no declaration and no indentation of its own. WriteTo writes UTF-8 to a stream or a buffer with the options you choose.

Patterns

Parse, change, write

Parse with XmlNode.Parse, change the tree, and write it with ToString or WriteTo. Comments and indentation inside the root element survive, and the elements you didn’t reach into are written from the parsed input.

Read with the document, edit a copy

Parse with XmlDocument when you need XmlDocumentOptions, a Stream, or the typed getters to find what to change. Find the element with GetChild and call CloneNode. The copy holds everything under the element and outlives the document.
The code produces the following output:

Adjust what the serializer produced

XmlSerializer.SerializeToNode gives you an object’s XML as a tree, and XmlSerializer.Deserialize accepts a node.
The code produces the following output:

Considerations

  • XML isn’t a property bag. An element’s children are an ordered list that can mix elements, text, comments, and more, and names can repeat. An element holds that list and a separate list of attributes, where a JsonObject holds one value per name.
  • What XmlNode.Parse keeps. The root element and everything inside it, including comments, the white space between elements, and whether an element with no content was <e/> or <e></e>. The XML declaration and anything outside the root element aren’t part of the tree.
  • A parsed tree is made as you reach into it. XmlNode.Parse and ParseValue keep the parsed input, and an element makes its attributes and child nodes the first time you reach into it. What the tree reads, copies, and writes is the same either way. The parsed input stays in memory while an element that nothing has reached into is reachable.
  • Little is validated until the tree is written. The constructors and name setters reject a null, empty, or white-space name. An invalid name, a duplicate attribute, a prefix without a namespace, a comment that contains --, or a CDATA section that contains ]]> throws an XmlException from ToString or WriteTo.
  • Values are strings. Convert them yourself, or read the XML with XmlDocument, whose XmlElement has typed getters such as GetInt32.
  • Nodes are ordinary objects. There is nothing to dispose, and a tree stays valid after the XmlDocument it was cloned from is disposed.
  • Several threads can read; one thread at a time can change. A tree that no thread is changing can be read from several threads at once, as a JsonNode tree can. That includes a parsed tree that is still being made as it’s reached into: every thread gets the same nodes. While any thread adds, removes, or changes a node, no other thread can use the tree, not even to read it. Use your own lock.
  • A list can’t change while it’s enumerated. Changing Children or Attributes inside a foreach over that list throws an InvalidOperationException at the loop’s next step. Loop over a copy, such as Children.ToArray().
  • CloudNimble.Text.Xml.Nodes.XmlNode
  • CloudNimble.Text.Xml.Nodes.XmlElementNode
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlElement
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlSerializer

Types

Classes