Table of Contents

Class XmlHelper

Namespace
SunamoXml
Assembly
SunamoXml.dll

Helper class for XML operations on XmlNode and XmlDocument, including attribute access, node search, formatting, and manipulation.

public static class XmlHelper
Inheritance
XmlHelper
Inherited Members

Properties

FoundAttribute

Stores the last attribute found by GetAttributeWithName(XmlNode, string).

public static XmlAttribute? FoundAttribute { get; set; }

Property Value

XmlAttribute

Methods

AddAttrsToRoot(ref XmlDocument, string, params string[])

Adds attributes to the document root, re-creating it with a new element name. Currently performs validation only.

public static void AddAttrsToRoot(ref XmlDocument xmlDocument, string newRootElementName, params string[] attributes)

Parameters

xmlDocument XmlDocument

The XML document to modify (passed by reference).

newRootElementName string

The name for the new root element.

attributes string[]

Alternating attribute names and values to add.

Attr(XmlNode, string)

Returns the value of the attribute with the specified name, or null if not found.

public static string? Attr(XmlNode node, string attributeName)

Parameters

node XmlNode

The XML node to search.

attributeName string

The attribute name to find.

Returns

string

ChildNodes(XmlNode)

Returns child nodes as a generic list since XmlNodeList only implements IEnumerable.

public static List<XmlNode> ChildNodes(XmlNode node)

Parameters

node XmlNode

The parent XML node.

Returns

List<XmlNode>

CreateXmlDocument(string)

Creates and returns a new XmlDocument with whitespace preservation from the specified XML content.

public static XmlDocument CreateXmlDocument(string content)

Parameters

content string

The XML content string to load.

Returns

XmlDocument

FormatXmlInMemory(string, string)

Formats XML content with indentation. Returns the formatted result or an error message on XML parse failure.

public static string FormatXmlInMemory(string xmlContent, string path = "")

Parameters

xmlContent string

The XML content to format.

path string

Optional file path for error context in exception messages.

Returns

string

GetAttrValueOrInnerElement(XmlNode, string)

Returns the attribute value or the inner text of a child element with the specified name.

public static string? GetAttrValueOrInnerElement(XmlNode node, string attributeName)

Parameters

node XmlNode

The XML node to search.

attributeName string

The attribute or child element name.

Returns

string

GetAttributeWithName(XmlNode, string)

Finds the first attribute with the specified name on the given node.

public static XmlNode? GetAttributeWithName(XmlNode node, string attributeName)

Parameters

node XmlNode

The XML node to search.

attributeName string

The attribute name to find.

Returns

XmlNode

GetAttributeWithNameValue(XmlNode, string)

Returns the inner XML value of the first attribute with the specified name.

public static string? GetAttributeWithNameValue(XmlNode node, string attributeName)

Parameters

node XmlNode

The XML node to search.

attributeName string

The attribute name to find.

Returns

string

GetChildNodeWithName(XmlNode, string)

Returns the first child node with the specified name, or null if not found.

public static XmlNode? GetChildNodeWithName(XmlNode node, string tagName)

Parameters

node XmlNode

The parent XML node to search.

tagName string

The child node name to find.

Returns

XmlNode

GetElementOfName(XmlNode, string)

Returns the first child element with the specified tag name.

public static XmlNode? GetElementOfName(XmlNode node, string tagName)

Parameters

node XmlNode

The parent XML node.

tagName string

The tag name to find.

Returns

XmlNode

GetElementsOfName(XmlNode, string)

Returns all child elements with the specified tag name.

public static IList<XmlNode> GetElementsOfName(XmlNode node, string tagName)

Parameters

node XmlNode

The parent XML node.

tagName string

The tag name to match.

Returns

IList<XmlNode>

GetInnerXml(XmlNode)

Returns the inner XML or CDATA value depending on the node type.

public static string GetInnerXml(XmlNode node)

Parameters

node XmlNode

The XML node to read.

Returns

string

InnerTextOfNode(XmlNode)

Returns the inner text of the specified XML node.

public static string InnerTextOfNode(XmlNode node)

Parameters

node XmlNode

The XML node to read.

Returns

string

IsXml(string)

Determines whether the string appears to be XML content by checking if it starts with a less-than sign.

public static bool IsXml(string text)

Parameters

text string

The string to check.

Returns

bool

RemoveAttrsFromRoot(ref XmlDocument, string, params string[])

Removes specified attributes from the document root and re-creates it with a new element name.

public static void RemoveAttrsFromRoot(ref XmlDocument xmlDocument, string newRootElementName, params string[] attributeNames)

Parameters

xmlDocument XmlDocument

The XML document to modify (passed by reference).

newRootElementName string

The name for the new root element.

attributeNames string[]

The attribute names to remove from the root.

ReplaceChildNodeByOuterHtml(XmlNode, XmlNode)

Replaces a child node by matching outer XML, working around the "node to be removed is not a child" error.

public static XmlNode ReplaceChildNodeByOuterHtml(XmlNode sourceNode, XmlNode targetNode)

Parameters

sourceNode XmlNode

The node to be replaced.

targetNode XmlNode

The replacement node.

Returns

XmlNode

SetAttribute(XmlNode, string, string)

Sets or creates an attribute with the specified name and value on the given node.

public static void SetAttribute(XmlNode node, string attributeName, string attributeValue)

Parameters

node XmlNode

The XML node to modify.

attributeName string

The name of the attribute to set.

attributeValue string

The value to assign to the attribute.