Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueTypeSyntax
Summary
Represents a specific XML node within an XmlDocument.Usage
AnXmlElement 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.
View the complete sample on GitHub
Remarks
This struct is the XML counterpart ofJsonElement 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.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.Read typed values
Compare GetString, GetInnerText, and GetRawXml
Read a nil element
Best Practices
- Keep the document alive while you use its elements. Don’t return an
XmlElementfrom a method that disposes the document or store one in a field that outlives it. Copy the value, or callCloneNode. - Test for all three element kinds. An element’s
ValueKindisElement,EmptyElement(for<e/>), orNil. A check forElementalone skips the other two. - Check
ValueKindwhen you enumerate. A document keeps comments and the white space between elements by default, so the children of an indented element includeTextnodes for the indentation, as well as any CDATA sections, comments, and processing instructions. - Read values from leaf elements.
GetStringandGetInnerTexton an element that has child elements include the white space between them. - Use the
Trylookups for optional names and theGetlookups 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
IsNilbefore a typed getter on an element that can carryxsi:nil. A typed getter on a nil element throws. - Use
GetDateTimeOffsetfor values with a time zone.GetDateTimeconverts an offset other thanZto local time. - Prefer a typed getter to parsing
GetStringyourself. 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. Aswitch on ValueKind handles each one.
Copy an element into the mutable tree
CloneNode returns a copy that owns its data and outlives the document.
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.
Considerations
Lifetime
- An
XmlElementholds 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.ValueKindreturnsUndefined, and every other member that reads a node throwsInvalidOperationException.- 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 ofris"\n x\n"and the value ofcis"x". GetStringreturns the value unchanged, white space included, andnullfor a nil element.GetInnerTextreturns 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.
GetBooleanacceptstrue,false,1, and0.GetDecimaldoesn’t accept an exponent.GetDoubleacceptsINF,-INF, andNaN. - A value that can’t be converted throws
XmlExceptionfrom a getter and returnsfalsefrom theTryGetmethod for the same type. ATryGetmethod still throws for a nil element and for a defaultXmlElement. - 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 ofGetString. GetDateTimereturnsDateTimeKind.Utcfor a value that ends inZ,Unspecifiedfor a value without a time zone, and local time for any other offset.GetDateTimeOffsetgives a value without a time zone the local offset.
Navigation
GetChildandTryGetChildsearch direct children and return the first match in document order. To search deeper, chain the calls or write a recursive method overEnumerateChildren. To reach a parent or a sibling, keep it in your own loop, or use the mutable tree, whose nodes have aParent.GetChild,TryGetChild, andGetAttributehave an overload that takes a namespace URI.TryGetAttributematches the local name in any namespace.EnumerateChildrenreturns every child node. With the default options that includes aTextnode for each run of white space between elements and aCommentnode for each comment.AttributeCountandEnumerateAttributesinclude namespace declarations (xmlnsandxmlns:prefix).- The enumerators are structs that work with
foreach. They don’t implementIEnumerable<T>, so LINQ doesn’t apply. - On a node that isn’t an element, the lookups find nothing, the enumerators are empty, and
LocalNameis an empty string (or the target, for a processing instruction).
Allocation
Name lookups, enumeration, the numeric, Boolean, date, and GUID getters, andWriteTo 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.ValueTypeSyntax
.ctor Inherited
Inherited from
objectSyntax
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 thexsi: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.ValueTypeSyntax
Parameters
Returns
Type:bool
Equals Inherited Virtual
Inherited from
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
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 withZ 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 literalsINF, -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.ValueTypeSyntax
Returns
Type:int
GetHashCode Inherited Virtual
Inherited from
objectSyntax
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 returnnull 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 literalsINF, -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
objectSyntax
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
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
ToString Override
Inherited from
System.ValueTypeSyntax
Returns
Type:string?
ToString Inherited Virtual
Inherited from
objectSyntax
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 returnsfalse 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 returnsfalse 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 returnsfalse 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 returnsfalse 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 returnsfalse 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 returnsfalse 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 returnsfalse 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 literalsINF, -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 literalsINF, -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.Related APIs
- 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