Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.NodesSyntax
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.
Parsereads a whole document from astringor from UTF-8 bytes and returns its root element.ParseValuereads the element aUtf8XmlReaderis positioned on.WriteTowrites the node to aUtf8XmlWriter, andToStringreturns its markup.DeepClonecopies the node and everything inside it.ValueKind,Parent, andNamespaceUriare available on every node.
<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 ofJsonNode 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.
Copy a node with DeepClone
Call ToString on nodes that aren’t elements
Best Practices
- Cast the result of
ParsetoXmlElementNode.ParseandParseValuereturn the base type, and the element members (Children,Attributes, the indexers) are onXmlElementNode. - Catch
XmlExceptionaroundParsefor input you don’t control. For a Boolean result instead, callXmlDocument.TryParseand thenCloneNode. - Use a type pattern to tell nodes apart.
child is XmlElementNode elementtests and casts in one step.ValueKindgives the same answer without the cast. - Parse through
XmlDocumentwhen you need options.XmlNode.Parsetakes none.XmlDocument.Parsewith anXmlDocumentOptions, followed byRootElement.CloneNode(), gives you limits, a tree without white space, and stream input. - Read
Valueto inspect a text node or an attribute.ToStringthrows for a node that can’t stand alone as markup.
Patterns
Read, then edit
UseXmlNode.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 withUtf8XmlReader, 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
CloudNimble.Text.Xml.Nodes namespace. A property pattern picks the elements with one name and skips text, comments, and everything else.
Considerations
Parsereturns the root element of the document. It doesn’t returnnull; input with no root element throws anXmlException.Parsekeeps 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>inXmlElementNode.IsEmptyElement. It drops the XML declaration and everything outside the root element.Parsetakes astringor UTF-8 bytes and no options, so the default limits ofXmlDocumentOptionsapply (for example, a depth of 64) and the document size isn’t limited. For aStreamor other options, parse withXmlDocumentand callXmlElement.CloneNode.ParseandParseValuekeep the parsed input with the tree. An element makes its attributes and child nodes the first time you reach into it, for example throughChildren,Attributes, orInnerText, 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.ParseValueuses 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.Parentisnulluntil the node is added to an element’sChildrenorAttributes, andnullagain after it’s removed.NamespaceUriisnullon 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.ToStringwrites with aUtf8XmlWriterthat 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 anXmlExceptionwhen 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.WriteToandToStringare where the tree is validated. Building the tree checks only that a name or a processing instruction target isn’tnull, empty, or white space.DeepCloneof 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
objectSyntax
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
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetType Inherited
Inherited from
objectSyntax
Returns
Type:System.Type
MemberwiseClone Inherited
Inherited from
objectSyntax
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
objectSyntax
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
objectSyntax
Returns
Type:string?
WriteTo Abstract
When overridden in a derived class, writes this node to the provided writer as XML.Syntax
Parameters
Exceptions
Related APIs
- 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