Summary
TheCloudNimble.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 anXmlElementNode and add to it, or parse existing XML with XmlNode.Parse. ToString returns the markup.
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
Add each kind of node
Best Practices
- Use the tree to change or build XML, and
XmlDocumentto 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.Parsemakes the nodes under an element when you first reach into it, throughChildren,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.CloneNodecopies 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
SetAttributeto add or change an attribute. It updates an attribute that exists.Attributes.Adddoesn’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
DeepCloneto use a node in two places. A node has one parent, and adding a node that already has one throws. - Write to a
Utf8XmlWriterfor anything but a quick look.ToStringbuilds a string with no declaration and no indentation of its own.WriteTowrites UTF-8 to a stream or a buffer with the options you choose.
Patterns
Parse, change, write
Parse withXmlNode.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 withXmlDocument 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.
Adjust what the serializer produced
XmlSerializer.SerializeToNode gives you an object’s XML as a tree, and XmlSerializer.Deserialize accepts a node.
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
JsonObjectholds one value per name. - What
XmlNode.Parsekeeps. 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.ParseandParseValuekeep 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 anXmlExceptionfromToStringorWriteTo. - Values are strings. Convert them yourself, or read the XML with
XmlDocument, whoseXmlElementhas typed getters such asGetInt32. - Nodes are ordinary objects. There is nothing to dispose, and a tree stays valid after the
XmlDocumentit 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
JsonNodetree 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
ChildrenorAttributesinside aforeachover that list throws anInvalidOperationExceptionat the loop’s next step. Loop over a copy, such asChildren.ToArray().
Related APIs
- 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