Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Nodes

Syntax

Summary

Defines the core behavior of a node in a mutable XML tree and provides a base for the classes that represent elements, attributes, text, CDATA sections, comments, processing instructions, and the XML declaration.

Usage

XmlNode is the base class of every node in the mutable tree. It’s the XML counterpart of JsonNode. You rarely create one directly: you create an XmlElementNode or one of the other derived types, or you call one of the static methods that parse XML into a tree.
  • Parse reads a whole document from a string or from UTF-8 bytes and returns its root element.
  • ParseValue reads the element a Utf8XmlReader is positioned on.
  • WriteTo writes the node to a Utf8XmlWriter, and ToString returns its markup.
  • DeepClone copies the node and everything inside it.
  • ValueKind, Parent, and NamespaceUri are available on every node.
The code produces the following output:
The tree keeps the root element with its comments and indentation, and <Notes></Notes> keeps its end tag. The declaration and the comment before the root aren’t part of it. View the complete sample on GitHub

Remarks

This class is the XML counterpart of JsonNode in System.Text.Json. Unlike XmlDocument, the tree can be changed, uses no pooled memory, and does not need to be disposed. A tree that no thread is changing can be read from several threads at once. Changes are not synchronized: while one thread changes a tree, no other thread can use it.

Examples

Parse one element from a reader

ParseValue leaves the reader on the end tag of the element it read, so the loop continues with the next one.
The code produces the following output:

Copy a node with DeepClone

The code produces the following output:
View the complete sample on GitHub

Call ToString on nodes that aren’t elements

The code produces the following output:
View the complete sample on GitHub

Best Practices

  • Cast the result of Parse to XmlElementNode. Parse and ParseValue return the base type, and the element members (Children, Attributes, the indexers) are on XmlElementNode.
  • Catch XmlException around Parse for input you don’t control. For a Boolean result instead, call XmlDocument.TryParse and then CloneNode.
  • Use a type pattern to tell nodes apart. child is XmlElementNode element tests and casts in one step. ValueKind gives the same answer without the cast.
  • Parse through XmlDocument when you need options. XmlNode.Parse takes none. XmlDocument.Parse with an XmlDocumentOptions, followed by RootElement.CloneNode(), gives you limits, a tree without white space, and stream input.
  • Read Value to inspect a text node or an attribute. ToString throws for a node that can’t stand alone as markup.

Patterns

Read, then edit

Use XmlNode.Parse when you intend to change the document, in one place or throughout: the tree makes nodes only for the elements you reach into. Use XmlDocument.Parse and XmlElement.CloneNode when you read the document with the typed getters of XmlElement and change a small part, or when you need XmlDocumentOptions or a Stream.

Edit one record at a time

Read a large input with Utf8XmlReader, call XmlNode.ParseValue for each record you need to change, write the changed record with WriteTo, and move on. Only one record is in the tree at a time.

Process nodes by type

This loop is part of the first example for the CloudNimble.Text.Xml.Nodes namespace. A property pattern picks the elements with one name and skips text, comments, and everything else.

Considerations

  • Parse returns the root element of the document. It doesn’t return null; input with no root element throws an XmlException.
  • Parse keeps comments and the white space between elements inside the root, and it records whether an element with no content was written as <e/> or <e></e> in XmlElementNode.IsEmptyElement. It drops the XML declaration and everything outside the root element.
  • Parse takes a string or UTF-8 bytes and no options, so the default limits of XmlDocumentOptions apply (for example, a depth of 64) and the document size isn’t limited. For a Stream or other options, parse with XmlDocument and call XmlElement.CloneNode.
  • Parse and ParseValue keep the parsed input with the tree. An element makes its attributes and child nodes the first time you reach into it, for example through Children, Attributes, or InnerText, and an element nothing has reached into is written and copied from the parsed input. What the tree reads and writes doesn’t depend on how much of it has been reached. The parsed input stays in memory while such an element is reachable, and it doesn’t depend on your string, bytes, or reader after the call returns.
  • ParseValue uses the options of the reader you pass. By default a reader reports comments and white-space-only text between elements, so the tree keeps both.
  • Parent is null until the node is added to an element’s Children or Attributes, and null again after it’s removed.
  • NamespaceUri is null on nodes that have no name (text, CDATA, comments, processing instructions, and the declaration). On an element or an attribute, it’s an empty string when the name is in no namespace.
  • ToString writes with a Utf8XmlWriter that omits the XML declaration, doesn’t indent, writes every line end as a line feed on every operating system, and writes by the rules of XML 1.1, because a node doesn’t know which version it was parsed from. A character that only XML 1.1 allows, or that XML 1.1 would normalize, is written as a character reference. It throws an XmlException when the node can’t be written as it stands: a text node or an attribute on its own, an invalid name, or a prefix with no namespace.
  • WriteTo and ToString are where the tree is validated. Building the tree checks only that a name or a processing instruction target isn’t null, empty, or white space.
  • DeepClone of an element that nothing has reached into shares the parsed input with the original, but the two are separate trees: a change to one doesn’t show in the other.

Constructors

.ctor Inherited

Inherited from object

Syntax

Properties

NamespaceUri Virtual

Gets or sets the namespace URI of this node.

Syntax

Property Value

Type: string? The namespace URI of the node. An element or an attribute in no namespace gives an empty string. For a node that is not an element or an attribute, the value is always null, and setting it has no effect.

Parent

Gets the parent of this node.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Nodes.XmlNode? The element that holds this node as a child or as an attribute, or null if this node has no parent.

ValueKind Abstract

When overridden in a derived class, gets the kind of this node.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlValueKind One of the enumeration values that indicates the kind of this node.

Methods

DeepClone Abstract

When overridden in a derived class, creates a new instance of the node. All children and attributes are recursively cloned.

Syntax

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode A new cloned instance of the current node. The clone has no parent.

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

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

Parse

Parses a string that holds a single XML document into a mutable tree.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode An XmlNode representation of the document element.

Exceptions

Remarks

Comments inside the document element are kept. The XML declaration and any comments or processing instructions outside the document element are not part of the returned tree.

Parse

Parses a span of UTF-8 bytes that holds a single XML document into a mutable tree.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode An XmlNode representation of the document element.

Exceptions

Remarks

Comments inside the document element are kept. The XML declaration and any comments or processing instructions outside the document element are not part of the returned tree.

ParseValue

Parses one XML element (including its attributes and descendants) from the provided reader into a mutable tree.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode An XmlNode representation of the element (and nested nodes) read from the reader.

Exceptions

Remarks

When this method returns, the reader is positioned on the end tag of the element, or on the element itself if it is an empty-element tag. Comments are kept only if the reader reports them.

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Override

Converts the current instance to a string that holds its XML markup.

Syntax

Returns

Type: string The XML markup of this node, including its attributes and descendants. No XML declaration is added.

Exceptions

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?

WriteTo Abstract

When overridden in a derived class, writes this node to the provided writer as XML.

Syntax

Parameters

Exceptions

  • CloudNimble.Text.Xml.Nodes.XmlElementNode
  • CloudNimble.Text.Xml.Nodes.XmlAttributeNode
  • CloudNimble.Text.Xml.Nodes.XmlTextNode
  • CloudNimble.Text.Xml.Nodes.XmlCDataNode
  • CloudNimble.Text.Xml.Nodes.XmlCommentNode
  • CloudNimble.Text.Xml.Nodes.XmlProcessingInstructionNode
  • CloudNimble.Text.Xml.Nodes.XmlDeclarationNode
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlElement
  • CloudNimble.Text.Xml.XmlValueKind
  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlSerializer