Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Nodes Inheritance: CloudNimble.Text.Xml.Nodes.XmlNodeSyntax
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:
SetAttributeadds an attribute or changes the value of one that exists.AddElementcreates a child element, appends it, and returns it.Addappends any node.- The indexer (
order["Item"]) returns the first child element with a local name, ornull. InnerTextgets the text inside the element, or replaces the element’s children with one text node.
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
Set, change, and remove attributes
Put elements and attributes in namespaces
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
SetAttributeto add or change an attribute. It never creates a duplicate. - Pass the namespace to
AddElementfor every element that belongs to one.feed.AddElement("title")under a namespaced parent creates atitlein no namespace, which is written withxmlns="". - 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,
xmlnsas its prefix, andXmlNamespaces.Xmlnsas its namespace. - Use the indexer for one child and a loop for many.
order["Item"]returns the first match only. - Read
InnerTextfrom 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
ValueKindis alwaysElement.IsEmptyElementsays how an element with no children is written: as an empty-element tag (<e/>) when it’strue, which is the default for an element you create, and as<e></e>when it’sfalse. A parsed element has the value its source had, andDeepClonecopies it. The property isn’t read while the element has children; such an element is written with an end tag.XmlWriterOptions.EmptyElementStylesets the form of the one tag,<e/>or<e />. WithStartAndEndTag, every element with no children is written as<e></e>, whateverIsEmptyElementis.- 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
nullwhen nothing matches. They search direct children only. SetAttributestores anullvalue as an empty string. It doesn’t remove the attribute.- Setting
InnerTextremoves every child, including child elements and comments, and adds one text node. Setting it tonullor an empty string leaves the element empty. LocalNamerejectsnull, 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;
ToStringandWriteTothrow anXmlExceptionwith the message “Prefixed names require a namespace URI.”. XmlElementNodeimplementsIEnumerable<XmlNode>overChildren, soforeachand LINQ work on it directly. Don’t changeChildrenwhile you enumerate it: the loop’s next step throws anInvalidOperationException. Loop over a copy (Children.ToArray()), or loop by index from the end.- An element of a tree from
XmlNode.ParseorParseValuemakes its attributes and children the first time you reach into it, for example throughChildren,Attributes, orInnerText, 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.XmlNodeSyntax
.ctor Inherited
Inherited from
objectSyntax
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.XmlNodeSyntax
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.XmlNodeSyntax
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.XmlNodeSyntax
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.XmlNodeSyntax
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
GetEnumerator
Syntax
Returns
Type:System.Collections.Generic.IEnumerator<CloudNimble.Text.Xml.Nodes.XmlNode>
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 Inherited
Inherited from
CloudNimble.Text.Xml.Nodes.XmlNodeSyntax
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.XmlNodeSyntax
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.XmlNodeSyntax
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
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 isnull, the new attribute is in no namespace.
SetParent Inherited
Inherited from
CloudNimble.Text.Xml.Nodes.XmlNodeSyntax
Parameters
ToString Override
Inherited from
CloudNimble.Text.Xml.Nodes.XmlNodeSyntax
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 Override
Syntax
Parameters
WriteTo Inherited Abstract
Inherited from
CloudNimble.Text.Xml.Nodes.XmlNodeSyntax
Parameters
Exceptions
Related APIs
- 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