Skip to main content

Definition

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

Syntax

Summary

Represents a mutable XML element with attributes and child nodes. This class cannot be inherited.

Usage

XmlElementNode is an element in the mutable tree. It has a name (LocalName, Prefix, NamespaceUri), a list of Attributes, and a list of Children that can hold any kind of node. The members you use most:
  • SetAttribute adds an attribute or changes the value of one that exists.
  • AddElement creates a child element, appends it, and returns it.
  • Add appends any node.
  • The indexer (order["Item"]) returns the first child element with a local name, or null.
  • InnerText gets the text inside the element, or replaces the element’s children with one text node.
The code produces the following output:
View the complete sample on GitHub

Remarks

The children can be elements, text nodes, CDATA sections, comments, and processing instructions, in any order. Enumerating the element enumerates XmlElementNode.Children.

Examples

Add and insert children

The code produces the following output:

Set, change, and remove attributes

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

Put elements and attributes in namespaces

The code produces the following output:
The writer declares each namespace where a name first needs it. Adding an xmlns:dc declaration attribute to the root, as the second half does, declares the prefix once. View the complete sample on GitHub

Best Practices

  • Use SetAttribute to add or change an attribute. It never creates a duplicate.
  • Pass the namespace to AddElement for every element that belongs to one. feed.AddElement("title") under a namespaced parent creates a title in no namespace, which is written with xmlns="".
  • Set a prefix only when the spelling matters. An element with a namespace and no prefix is written with a default namespace declaration. An attribute with a namespace and no prefix gets a generated prefix such as ns1.
  • Declare a shared prefix once on an ancestor. Add an attribute with the prefix as its local name, xmlns as its prefix, and XmlNamespaces.Xmlns as its namespace.
  • Use the indexer for one child and a loop for many. order["Item"] returns the first match only.
  • Read InnerText from the element that holds the value. On an element with child elements it joins all the text below it, including the white space between the children in a tree parsed from an indented document.
  • Add your own white space when the layout matters. A node you add to an indented tree isn’t indented. Either add text nodes for the line break and indentation, or work on a tree without white space and write it with an indenting Utf8XmlWriter.

Considerations

  • ValueKind is always Element. IsEmptyElement says how an element with no children is written: as an empty-element tag (<e/>) when it’s true, which is the default for an element you create, and as <e></e> when it’s false. A parsed element has the value its source had, and DeepClone copies it. The property isn’t read while the element has children; such an element is written with an end tag.
  • XmlWriterOptions.EmptyElementStyle sets the form of the one tag, <e/> or <e />. With StartAndEndTag, every element with no children is written as <e></e>, whatever IsEmptyElement is.
  • The one-argument indexer matches a local name in any namespace. SetAttribute(name, value) sets the attribute in no namespace. The overloads that take a namespace URI match that namespace only; pass an empty string for no namespace.
  • The indexers return null when nothing matches. They search direct children only.
  • SetAttribute stores a null value as an empty string. It doesn’t remove the attribute.
  • Setting InnerText removes every child, including child elements and comments, and adds one text node. Setting it to null or an empty string leaves the element empty.
  • LocalName rejects null, an empty string, and white space when it’s set. Whether the name is a valid XML name is checked only when the element is written.
  • The element’s namespace comes from its NamespaceUri. When the element is written, a namespace declaration attribute for the element’s own prefix is written with the element’s namespace, even if the attribute’s value says something else.
  • A prefix with no namespace can’t be written; ToString and WriteTo throw an XmlException with the message “Prefixed names require a namespace URI.”.
  • XmlElementNode implements IEnumerable<XmlNode> over Children, so foreach and LINQ work on it directly. Don’t change Children while you enumerate it: the loop’s next step throws an InvalidOperationException. Loop over a copy (Children.ToArray()), or loop by index from the end.
  • An element of a tree from XmlNode.Parse or ParseValue makes its attributes and children the first time you reach into it, for example through Children, Attributes, or InnerText, or by changing it. Until then it’s written and copied from the parsed input. Nothing you read or write depends on whether that has happened.

Constructors

.ctor

Initializes a new instance of the XmlElementNode class with the specified local name, namespace URI, and prefix.

Syntax

Parameters

Exceptions

Remarks

The new element has no attributes, no children, and no parent.

.ctor Inherited

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

Attributes

Gets the attributes of this element.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Nodes.XmlAttributeNodeList The list of attributes of this element, in the order they are written. The list can be changed.

Children

Gets the child nodes of this element.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Nodes.XmlNodeList The list of child nodes of this element, in document order. The list can be changed.

InnerText

Gets or sets the text content of this element.

Syntax

Property Value

Type: string The text of all text nodes and CDATA sections inside this element, in document order. Setting the value removes all children and adds one text node that holds the value. Setting null or an empty string removes all children and adds nothing.

IsEmptyElement

Gets or sets a value that indicates whether this element is written as one tag when it has no children.

Syntax

Property Value

Type: bool true if the element is written as an empty-element tag (<e/>) when it has no children; false if it is written as a start tag and an end tag (<e></e>). The default is true. An element that is parsed has the value its source had: false for an element that had an end tag.

Remarks

The value is not read while the element has children. The form of the one tag, <e/> or <e />, and whether every element with no content is written with an end tag, is set by XmlWriterOptions.EmptyElementStyle.

LocalName

Gets or sets the local name of this element.

Syntax

Property Value

Type: string The local name of the element, without a prefix.

Exceptions

NamespaceUri Override

Gets or sets the namespace URI of this element.

Syntax

Property Value

Type: string? The namespace URI of the element, or an empty string if the element is in no namespace. The value is never null. Setting null stores an empty string.

NamespaceUri Inherited Virtual

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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 Inherited

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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.

Prefix

Gets or sets the namespace prefix of this element.

Syntax

Property Value

Type: string The prefix of the element name, or an empty string if the name has no prefix. Setting null stores an empty string.

this[]

Gets the first child element that has the specified local name, in any namespace.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Nodes.XmlElementNode? The first child element that has the specified local name, or null if there is none.

Exceptions

Remarks

Only direct children are searched, not deeper descendants.

this[]

Gets the first child element that has the specified local name and namespace URI.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Nodes.XmlElementNode? The first child element that has the specified local name and namespace URI, or null if there is none.

Exceptions

Remarks

Only direct children are searched, not deeper descendants.

ValueKind Override

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlValueKind

ValueKind Inherited Abstract

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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

Add

Adds the specified node to the end of the child nodes of this element.

Syntax

Parameters

Exceptions

AddElement

Creates an element with the specified local name and namespace URI and adds it to the end of the child nodes of this element.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlElementNode The new child element. It has no prefix.

Exceptions

DeepClone Override

Syntax

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode

DeepClone Inherited Abstract

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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

GetEnumerator

Syntax

Returns

Type: System.Collections.Generic.IEnumerator<CloudNimble.Text.Xml.Nodes.XmlNode>

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 Inherited

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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 Inherited

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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 Inherited

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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

SetAttribute

Sets the value of the attribute that has the specified name, or adds the attribute if this element does not have it.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlAttributeNode The attribute that was changed, or the new attribute that was added.

Exceptions

Remarks

The attribute is found by its local name and its namespace together, so an attribute with the same local name in another namespace is left as it is. An attribute that this method adds has no prefix. When namespaceUri is null, the new attribute is in no namespace.

SetParent Inherited

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
Sets the parent pointer.

Syntax

Parameters

ToString Override

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
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 Override

Syntax

Parameters

WriteTo Inherited Abstract

Inherited from CloudNimble.Text.Xml.Nodes.XmlNode
When overridden in a derived class, writes this node to the provided writer as XML.

Syntax

Parameters

Exceptions

  • CloudNimble.Text.Xml.Nodes.XmlNode
  • CloudNimble.Text.Xml.Nodes.XmlNodeList
  • CloudNimble.Text.Xml.Nodes.XmlAttributeNodeList
  • CloudNimble.Text.Xml.Nodes.XmlAttributeNode
  • CloudNimble.Text.Xml.Nodes.XmlTextNode
  • CloudNimble.Text.Xml.XmlNamespaces
  • CloudNimble.Text.Xml.XmlElement
  • CloudNimble.Text.Xml.Utf8XmlWriter