Table of Contents

Class XmlHelper

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

Provides helper extension methods for working with XML elements, attributes, and XML strings.

public static class XmlHelper
Inheritance
XmlHelper
Inherited Members

Methods

Compare(XmlNode, XmlNode)

Compares two XmlNode objects based on their OuterXml representation.

public static bool Compare(this XmlNode first, XmlNode second)

Parameters

first XmlNode

The first XmlNode to compare.

second XmlNode

The second XmlNode to compare.

Returns

bool

True if both XmlNodes have identical OuterXml representations; otherwise, false.

Exceptions

ArgumentNullException

Thrown when the first or second XmlNode is null.

GetAttributeValue<T>(XElement, XName, T)

Retrieves the value of the attribute with the specified name and converts it to the specified type.

public static T GetAttributeValue<T>(this XElement elem, XName name, T defaultValue = default)

Parameters

elem XElement

The XML element containing the attribute.

name XName

The name of the attribute.

defaultValue T

The default value to return if the attribute is not found.

Returns

T

The converted value of the attribute, or the default value if the attribute does not exist.

Type Parameters

T

The type to which the attribute's value should be converted.

Exceptions

ArgumentNullException

Thrown when the XML element is null.

GetElementValue<T>(XElement, XName, T)

Retrieves the value of the child element with the specified name and converts it to the specified type.

public static T GetElementValue<T>(this XElement parent, XName name, T defaultValue = default)

Parameters

parent XElement

The parent XML element.

name XName

The name of the child element.

defaultValue T

The default value to return if the element is not found.

Returns

T

The converted value of the child element, or the default value if the element does not exist.

Type Parameters

T

The type to which the element's value should be converted.

Exceptions

ArgumentNullException

Thrown when the parent element is null.

IsXmlChar(char)

Determines whether the specified character is a valid XML character.

public static bool IsXmlChar(this char c)

Parameters

c char

The character to check.

Returns

bool

True if the character is a valid XML character; otherwise, false.

IsXmlString(string)

Determines whether the specified string contains only valid XML characters.

public static bool IsXmlString(this string value)

Parameters

value string

The string to check.

Returns

bool

True if all characters in the string are valid XML characters; otherwise, false.

WriteAttribute(XmlWriter, string, object)

Writes an attribute with the specified name and value to the XmlWriter.

public static XmlWriter WriteAttribute(this XmlWriter writer, string name, object value)

Parameters

writer XmlWriter

The XML writer.

name string

The name of the attribute to write.

value object

The value of the attribute. If null, an empty string is written.

Returns

XmlWriter

The same instance of the XmlWriter to allow for method chaining.

Exceptions

ArgumentNullException

Thrown when the XmlWriter is null.