Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueType

Syntax

Summary

Represents a specific XML node within an XmlDocument.

Usage

An XmlElement is one node of an XmlDocument: an element, an attribute, a text node, a CDATA section, a comment, or a processing instruction. ValueKind says which. It’s the XML counterpart of JsonElement: a small struct that points into the document and is usable only while the document is alive. You get the first one from XmlDocument.RootElement. From there, GetChild and GetAttribute find a node by name and throw an XmlException when it’s missing, and TryGetChild and TryGetAttribute return false instead. The typed getters convert a value.
The code produces the following output:
The members fall into these groups: View the complete sample on GitHub

Remarks

This struct is the XML counterpart of JsonElement in System.Text.Json. A value can represent an element, an attribute, a text node, a CDATA section, a comment, or a processing instruction; XmlElement.ValueKind tells which. A value is usable only while its parent XmlDocument is not disposed. The value of an attribute, a text node, a CDATA section, a comment, or a processing instruction is its content. The value of an element is the text of all text nodes and CDATA sections inside it, in document order. The typed getters, such as XmlElement.GetInt32, ignore leading and trailing white space in the value. Name lookups, enumeration, the numeric and Boolean getters, and XmlElement.WriteTo do not allocate. Members that return a String, a byte[], or an XmlNode allocate only that result, except XmlElement.GetRawXml, which also allocates the buffer and the writer it writes the markup with.

Examples

Enumerate children and attributes

A document keeps the white space between elements and the comments, so a loop over the children skips what isn’t an element.
The code produces the following output:

Find names in a namespace

The one-argument lookups match a local name in any namespace. Pass a namespace URI to match one namespace, or an empty string for a name in no namespace.
The code produces the following output:

Read typed values

The code produces the following output:

Compare GetString, GetInnerText, and GetRawXml

The code produces the following output:

Read a nil element

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

Best Practices

  • Keep the document alive while you use its elements. Don’t return an XmlElement from a method that disposes the document or store one in a field that outlives it. Copy the value, or call CloneNode.
  • Test for all three element kinds. An element’s ValueKind is Element, EmptyElement (for <e/>), or Nil. A check for Element alone skips the other two.
  • Check ValueKind when you enumerate. A document keeps comments and the white space between elements by default, so the children of an indented element include Text nodes for the indentation, as well as any CDATA sections, comments, and processing instructions.
  • Read values from leaf elements. GetString and GetInnerText on an element that has child elements include the white space between them.
  • Use the Try lookups for optional names and the Get lookups for names the document must have, so that a missing required name fails with a message that names it.
  • Pass the namespace URI when a document mixes vocabularies. The one-argument lookups return the first match in any namespace.
  • Check IsNil before a typed getter on an element that can carry xsi:nil. A typed getter on a nil element throws.
  • Use GetDateTimeOffset for values with a time zone. GetDateTime converts an offset other than Z to local time.
  • Prefer a typed getter to parsing GetString yourself. The getters apply the XML Schema lexical rules, don’t depend on the current culture, and don’t allocate a string.

Patterns

Switch on the kind of each child

Content you don’t control can hold any kind of node. A switch on ValueKind handles each one.
The code produces the following output:

Copy an element into the mutable tree

CloneNode returns a copy that owns its data and outlives the document.
The code produces the following output:

Write an element inside new markup

WriteTo writes the node and everything in it. The writer adds a declaration for any namespace that an ancestor outside the copied part declared.
The code produces the following output:
View the complete sample on GitHub

Considerations

Lifetime

  • An XmlElement holds a reference to its document and a position in it. It holds no data of its own.
  • After the document is disposed, every member that reads from it throws ObjectDisposedException.
  • default(XmlElement) refers to no node. ValueKind returns Undefined, and every other member that reads a node throws InvalidOperationException.
  • Two values are equal when they point at the same node of the same document.

Values

  • The value of an attribute, a text node, a CDATA section, a comment, or a processing instruction is its content. The value of an element is the text of every text node and CDATA section inside it, at any depth, in document order.
  • The text of an element that has child elements includes the white space between them when the document keeps it, which is the default. For <r>, a line break, two spaces, x, a line break, and </r>, the value of r is "\n x\n" and the value of c is "x".
  • GetString returns the value unchanged, white space included, and null for a nil element. GetInnerText returns an empty string for a nil element, a comment, or a processing instruction.
  • The typed getters ignore white space around the value and follow the XML Schema lexical forms. GetBoolean accepts true, false, 1, and 0. GetDecimal doesn’t accept an exponent. GetDouble accepts INF, -INF, and NaN.
  • A value that can’t be converted throws XmlException from a getter and returns false from the TryGet method for the same type. A TryGet method still throws for a nil element and for a default XmlElement.
  • The getters are those of JsonElement: bool, the eight integer types, float, double, decimal, DateTime, DateTimeOffset, Guid, and Base64 bytes. For another type, convert the result of GetString.
  • GetDateTime returns DateTimeKind.Utc for a value that ends in Z, Unspecified for a value without a time zone, and local time for any other offset. GetDateTimeOffset gives a value without a time zone the local offset.
  • GetChild and TryGetChild search direct children and return the first match in document order. To search deeper, chain the calls or write a recursive method over EnumerateChildren. To reach a parent or a sibling, keep it in your own loop, or use the mutable tree, whose nodes have a Parent.
  • GetChild, TryGetChild, and GetAttribute have an overload that takes a namespace URI. TryGetAttribute matches the local name in any namespace.
  • EnumerateChildren returns every child node. With the default options that includes a Text node for each run of white space between elements and a Comment node for each comment.
  • AttributeCount and EnumerateAttributes include namespace declarations (xmlns and xmlns:prefix).
  • The enumerators are structs that work with foreach. They don’t implement IEnumerable<T>, so LINQ doesn’t apply.
  • On a node that isn’t an element, the lookups find nothing, the enumerators are empty, and LocalName is an empty string (or the target, for a processing instruction).

Allocation

Name lookups, enumeration, the numeric, Boolean, date, and GUID getters, and WriteTo don’t allocate. LocalName, Prefix, NamespaceUri, GetString, and GetInnerText allocate the string they return, GetBytesFromBase64 allocates the array, and CloneNode allocates the copy. GetRawXml writes the node with a Utf8XmlWriter into a buffer of its own and returns the result as a string.

Markup

GetRawXml writes the node with a Utf8XmlWriter; it isn’t a slice of the original input. It takes no options, so its line ends are line feeds on every operating system. It returns an empty string for an attribute. IsEmptyElement is true for <e/> and false for <e></e>, and WriteTo keeps the difference.

Constructors

.ctor Inherited

Inherited from System.ValueType

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

AttributeCount

Gets the number of attributes on this element.

Syntax

Property Value

Type: int The number of attributes on this element, including namespace declarations, or 0 if this node is not an element.

Exceptions

IsEmptyElement

Gets a value that indicates whether this node is an element that is written as an empty-element tag (<e/>).

Syntax

Property Value

Type: bool true if this node is an element that is written as an empty-element tag; otherwise, false. An element with a start tag and an end tag and no content (<e></e>) gives false.

Exceptions

IsNil

Gets a value that indicates whether this node is an element that is marked as nil with the xsi:nil attribute.

Syntax

Property Value

Type: bool true if this node is an element whose xsi:nil attribute is true or 1; otherwise, false.

Exceptions

LocalName

Gets the local name of this node.

Syntax

Property Value

Type: string The local name of an element or an attribute, without its prefix, or the target of a processing instruction. The value is an empty string for a node that has no name.

Exceptions

NamespaceUri

Gets the namespace URI of this node.

Syntax

Property Value

Type: string The namespace URI of the element or attribute, or an empty string if the node is in no namespace.

Exceptions

Prefix

Gets the namespace prefix of this node.

Syntax

Property Value

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

Exceptions

TokenType

Gets the reader token type that corresponds to this node.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlTokenType One of the enumeration values that indicates the token a Utf8XmlReader reports for this kind of node. An element gives XmlTokenType.StartElement.

Exceptions

ValueKind

Gets the kind of the current XML node.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlValueKind One of the enumeration values that indicates the kind of this node. A default XmlElement gives XmlValueKind.Undefined.

Exceptions

Methods

CloneNode

Creates a mutable copy of this node and all of its attributes and descendants.

Syntax

Returns

Type: CloudNimble.Text.Xml.Nodes.XmlNode A new XmlNode that has no parent. Its type matches the kind of this node, for example an XmlElementNode for an element.

Exceptions

Remarks

The copy does not depend on the parent XmlDocument and stays valid after the document is disposed.

EnumerateAttributes

Gets an enumerator to enumerate the attributes of this element.

Syntax

Returns

Type: CloudNimble.Text.Xml.XmlElement.AttributeEnumerator An enumerator to enumerate the attributes of this element, in document order. Namespace declarations are included. The enumerator yields nothing if this node is not an element.

Exceptions

EnumerateChildren

Gets an enumerator to enumerate the child nodes of this element.

Syntax

Returns

Type: CloudNimble.Text.Xml.XmlElement.Enumerator An enumerator to enumerate the child nodes of this element, in document order. Child nodes are elements, text nodes, CDATA sections, comments, and processing instructions. Attributes are not included. The enumerator yields nothing if this node is not an element.

Exceptions

Equals

Syntax

Parameters

Returns

Type: bool

Equals Override

Syntax

Parameters

Returns

Type: bool

Equals Override

Inherited from System.ValueType

Syntax

Parameters

Returns

Type: bool

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetAttribute

Gets the first attribute of this element that has the specified local name, in any namespace.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the attribute.

Exceptions

GetAttribute

Gets the attribute of this element that has the specified local name and namespace URI.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the attribute.

Exceptions

GetAttribute

Gets the first attribute of this element that has the local name specified as a read-only span of characters, in any namespace.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the attribute.

Exceptions

GetAttribute

Gets the first attribute of this element that has the local name specified as a read-only span of UTF-8 bytes, in any namespace.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the attribute.

Exceptions

GetBoolean

Gets the value of this node as a Boolean.

Syntax

Returns

Type: bool The value of this node as a Boolean.

Exceptions

GetByte

Gets the value of this node as a Byte.

Syntax

Returns

Type: byte The value of this node as a Byte.

Exceptions

GetBytesFromBase64

Gets the value of this node as a byte array, decoded from Base64 text.

Syntax

Returns

Type: byte[] The value decoded as a byte array.

Exceptions

GetChild

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

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the child element.

Exceptions

Remarks

Only direct children are searched, not deeper descendants.

GetChild

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

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the child element.

Exceptions

Remarks

Only direct children are searched, not deeper descendants.

GetChild

Gets the first child element of this element that has the local name specified as a read-only span of characters, in any namespace.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the child element.

Exceptions

Remarks

Only direct children are searched, not deeper descendants.

GetChild

Gets the first child element of this element that has the local name specified as a read-only span of UTF-8 bytes, in any namespace.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlElement An XmlElement that represents the child element.

Exceptions

Remarks

Only direct children are searched, not deeper descendants.

GetDateTime

Gets the value of this node as a DateTime.

Syntax

Returns

Type: System.DateTime The value of this node as a DateTime.

Exceptions

Remarks

A value with no time zone gives Unspecified. A value with Z or a zero offset gives Utc. A value with a non-zero offset is converted to local time and gives Local.

GetDateTimeOffset

Gets the value of this node as a DateTimeOffset.

Syntax

Returns

Type: System.DateTimeOffset The value of this node as a DateTimeOffset.

Exceptions

Remarks

A value with no time zone takes the local offset that is in force at that date and time.

GetDecimal

Gets the value of this node as a Decimal.

Syntax

Returns

Type: decimal The value of this node as a Decimal.

Exceptions

GetDouble

Gets the value of this node as a Double.

Syntax

Returns

Type: double The value of this node as a Double.

Exceptions

Remarks

The literals INF, -INF, and NaN are accepted in any letter case.

GetGuid

Gets the value of this node as a Guid.

Syntax

Returns

Type: System.Guid The value of this node as a Guid.

Exceptions

GetHashCode Override

Syntax

Returns

Type: int

GetHashCode Override

Inherited from System.ValueType

Syntax

Returns

Type: int

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetInnerText

Gets the text content of this node.

Syntax

Returns

Type: string For an element, the text of all text nodes and CDATA sections inside it, in document order. For an attribute, a text node, or a CDATA section, its value. For a comment or a processing instruction, an empty string.

Exceptions

Remarks

Unlike XmlElement.GetString, this method does not return null for an element that is marked as nil.

GetInt16

Gets the value of this node as a Int16.

Syntax

Returns

Type: short The value of this node as a Int16.

Exceptions

GetInt32

Gets the value of this node as an Int32.

Syntax

Returns

Type: int The value of this node as an Int32.

Exceptions

GetInt64

Gets the value of this node as a Int64.

Syntax

Returns

Type: long The value of this node as a Int64.

Exceptions

GetRawXml

Gets a string that holds this node written as XML.

Syntax

Returns

Type: string The XML markup of this node, including its attributes and descendants. The result is an empty string for an attribute, because an attribute has no markup outside its element.

Exceptions

Remarks

The markup is written again from the parsed node with XmlElement.WriteTo, without an XML declaration. It is not a copy of the original input.

GetSByte

Gets the value of this node as an SByte.

Syntax

Returns

Type: sbyte The value of this node as an SByte.

Exceptions

GetSingle

Gets the value of this node as a Single.

Syntax

Returns

Type: float The value of this node as a Single.

Exceptions

Remarks

The literals INF, -INF, and NaN are accepted in any letter case.

GetString

Gets the value of this node as a String.

Syntax

Returns

Type: string? The value of this node as a String, or null if this node is an element that is marked as nil.

Exceptions

Remarks

Entity and character references in the value are expanded. Leading and trailing white space is kept.

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

GetUInt16

Gets the value of this node as a UInt16.

Syntax

Returns

Type: ushort The value of this node as a UInt16.

Exceptions

GetUInt32

Gets the value of this node as a UInt32.

Syntax

Returns

Type: uint The value of this node as a UInt32.

Exceptions

GetUInt64

Gets the value of this node as a UInt64.

Syntax

Returns

Type: ulong The value of this node as a UInt64.

Exceptions

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Override

Inherited from System.ValueType

Syntax

Returns

Type: string?

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?

TryGetAttribute

Looks for the first attribute of this element that has the specified local name, in any namespace.

Syntax

Parameters

Returns

Type: bool true if the attribute was found; otherwise, false.

Exceptions

Remarks

This method returns false if this node is not an element.

TryGetAttribute

Looks for the first attribute of this element that has the local name specified as a read-only span of characters, in any namespace.

Syntax

Parameters

Returns

Type: bool true if the attribute was found; otherwise, false.

Exceptions

Remarks

This method returns false if this node is not an element.

TryGetAttribute

Looks for the first attribute of this element that has the local name specified as a read-only span of UTF-8 bytes, in any namespace.

Syntax

Parameters

Returns

Type: bool true if the attribute was found; otherwise, false.

Exceptions

Remarks

This method returns false if this node is not an element.

TryGetByte

Attempts to get the value of this node as a Byte.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:unsignedByte; otherwise, false.

Exceptions

TryGetBytesFromBase64

Attempts to get the value of this node as a byte array, decoded from Base64 text.

Syntax

Parameters

Returns

Type: bool true if the whole value is valid Base64 text (xs:base64Binary); otherwise, false.

Exceptions

TryGetChild

Looks for the first child element of this element that has the specified local name, in any namespace.

Syntax

Parameters

Returns

Type: bool true if the child element was found; otherwise, false.

Exceptions

Remarks

Only direct children are searched, not deeper descendants. This method returns false if this node is not an element.

TryGetChild

Looks for the first child element of this element that has the specified local name and namespace URI.

Syntax

Parameters

Returns

Type: bool true if the child element was found; otherwise, false.

Exceptions

Remarks

Only direct children are searched, not deeper descendants. This method returns false if this node is not an element.

TryGetChild

Looks for the first child element of this element that has the local name specified as a read-only span of characters, in any namespace.

Syntax

Parameters

Returns

Type: bool true if the child element was found; otherwise, false.

Exceptions

Remarks

Only direct children are searched, not deeper descendants. This method returns false if this node is not an element.

TryGetChild

Looks for the first child element of this element that has the local name specified as a read-only span of UTF-8 bytes, in any namespace.

Syntax

Parameters

Returns

Type: bool true if the child element was found; otherwise, false.

Exceptions

Remarks

Only direct children are searched, not deeper descendants. This method returns false if this node is not an element.

TryGetDateTime

Attempts to get the value of this node as a DateTime.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:dateTime that a DateTime can represent; otherwise, false.

Exceptions

Remarks

The value is read as it is by XmlElement.GetDateTime.

TryGetDateTimeOffset

Attempts to get the value of this node as a DateTimeOffset.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:dateTime that a DateTimeOffset can represent; otherwise, false.

Exceptions

Remarks

The value is read as it is by XmlElement.GetDateTimeOffset.

TryGetDecimal

Attempts to get the value of this node as a Decimal.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:decimal that a Decimal can represent; otherwise, false.

Exceptions

TryGetDouble

Attempts to get the value of this node as a Double.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:double; otherwise, false.

Exceptions

Remarks

The literals INF, -INF, and NaN are accepted in any letter case.

TryGetGuid

Attempts to get the value of this node as a Guid.

Syntax

Parameters

Returns

Type: bool true if the whole value is a GUID in the D, N, B, or P format; otherwise, false.

Exceptions

TryGetInt16

Attempts to get the value of this node as a Int16.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:short; otherwise, false.

Exceptions

TryGetInt32

Attempts to get the value of this node as an Int32.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:int; otherwise, false.

Exceptions

TryGetInt64

Attempts to get the value of this node as a Int64.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:long; otherwise, false.

Exceptions

TryGetSByte

Attempts to get the value of this node as an SByte.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:byte; otherwise, false.

Exceptions

TryGetSingle

Attempts to get the value of this node as a Single.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:float; otherwise, false.

Exceptions

Remarks

The literals INF, -INF, and NaN are accepted in any letter case.

TryGetUInt16

Attempts to get the value of this node as a UInt16.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:unsignedShort; otherwise, false.

Exceptions

TryGetUInt32

Attempts to get the value of this node as a UInt32.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:unsignedInt; otherwise, false.

Exceptions

TryGetUInt64

Attempts to get the value of this node as a UInt64.

Syntax

Parameters

Returns

Type: bool true if the whole value is an xs:unsignedLong; otherwise, false.

Exceptions

WriteTo

Writes this node to the specified writer as XML.

Syntax

Parameters

Exceptions

Remarks

An element is written with its attributes and descendants. Nothing is written for an attribute, because an attribute has no markup outside its element.
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlValueKind
  • CloudNimble.Text.Xml.XmlTokenType
  • CloudNimble.Text.Xml.XmlAttributes
  • CloudNimble.Text.Xml.XmlNamespaces
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlSerializer
  • CloudNimble.Text.Xml.XmlException
  • CloudNimble.Text.Xml.Nodes.XmlNode
  • CloudNimble.Text.Xml.Nodes.XmlElementNode