Class XmlHelper
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
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
XElementThe XML element containing the attribute.
name
XNameThe name of the attribute.
defaultValue
TThe 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
XElementThe parent XML element.
name
XNameThe name of the child element.
defaultValue
TThe 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
charThe 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
stringThe 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
XmlWriterThe XML writer.
name
stringThe name of the attribute to write.
value
objectThe 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.