Table of Contents

Class XHelper

Namespace
SunamoXml
Assembly
SunamoXml.dll

Helper class for XElement-based XML operations including namespace management, element search, attribute access, and XML minification.

public class XHelper
Inheritance
XHelper
Inherited Members
Extension Methods

Properties

Namespaces

Dictionary mapping namespace prefixes to their URI values.

public static Dictionary<string, string> Namespaces { get; set; }

Property Value

Dictionary<string, string>

Methods

AddXmlNamespaces(Dictionary<string, string>)

Adds all entries from a dictionary to the shared Namespaces dictionary.

public static void AddXmlNamespaces(Dictionary<string, string> dictionary)

Parameters

dictionary Dictionary<string, string>

The dictionary of prefix-URI pairs to add.

AddXmlNamespaces(params string[])

Adds namespace declarations from alternating prefix-URI pairs.

public static void AddXmlNamespaces(params string[] namespacePairs)

Parameters

namespacePairs string[]

Alternating namespace prefix (with "xmlns:" prefix) and URI values.

AddXmlNamespaces(XmlNamespaceManager)

Adds all namespace declarations from an XmlNamespaceManager to the shared Namespaces dictionary.

public static void AddXmlNamespaces(XmlNamespaceManager namespaceManager)

Parameters

namespaceManager XmlNamespaceManager

The namespace manager to read from.

Attr(XElement, string)

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

public static string? Attr(XElement element, string attributeName)

Parameters

element XElement

The XElement to search.

attributeName string

The attribute name to find.

Returns

string

CreateXDocument(string)

Creates an XDocument from an XML string or file path.

public static Task<XDocument> CreateXDocument(string contentOrFilePath)

Parameters

contentOrFilePath string

The XML content string or file path.

Returns

Task<XDocument>

FormatXml(string)

Formats XML content with proper indentation. If input is a file path, saves the result back to the file and returns null. Otherwise returns the formatted string.

public static Task<string?> FormatXml(string pathOrContent)

Parameters

pathOrContent string

The file path or XML content string.

Returns

Task<string>

FormatXmlInMemory(string)

Formats XML content in memory using XDocument parsing. Returns the original string on parse failure.

public static string FormatXmlInMemory(string xml)

Parameters

xml string

The XML string to format.

Returns

string

GetElementOfName(XContainer, string)

Finds an element by name within the container. Supports namespace-prefixed tag names (e.g., "ns:tag").

public static XElement? GetElementOfName(XContainer node, string tagName)

Parameters

node XContainer

The container to search.

tagName string

The tag name to find.

Returns

XElement

GetElementOfNameRecursive(XElement, string)

Finds the first descendant element (including self) with the specified tag name. Supports namespace-prefixed tag names.

public static XElement? GetElementOfNameRecursive(XElement node, string tagName)

Parameters

node XElement

The XElement to search recursively.

tagName string

The tag name to find.

Returns

XElement

GetElementOfNameWithAttr(XElement, string, string, string)

Finds an element by tag name that also has an attribute with the specified value. Supports namespace-prefixed tag names.

public static XElement? GetElementOfNameWithAttr(XElement node, string tagName, string attributeName, string attributeValue)

Parameters

node XElement

The parent XElement to search.

tagName string

The tag name to match, optionally with namespace prefix (e.g., "ns:tag").

attributeName string

The attribute name to match.

attributeValue string

The expected attribute value.

Returns

XElement

GetElementOfSecondLevel(XElement, string, string)

Returns the child element found at the second nesting level (firstLevelName/secondLevelName).

public static XElement? GetElementOfSecondLevel(XElement parentElement, string firstLevelName, string secondLevelName)

Parameters

parentElement XElement

The root XElement to search from.

firstLevelName string

The first-level element name.

secondLevelName string

The second-level element name.

Returns

XElement

GetElementsOfName(XElement, string)

Returns all direct child elements matching the specified tag name. Supports namespace-prefixed tag names.

public static List<XElement> GetElementsOfName(XElement node, string tagName)

Parameters

node XElement

The parent XElement to search.

tagName string

The tag name to match.

Returns

List<XElement>

GetElementsOfNameRecursive(XElement, string)

Returns all descendant elements (including self) matching the specified tag name. Supports namespace-prefixed tag names.

public static List<XElement> GetElementsOfNameRecursive(XElement node, string tagName)

Parameters

node XElement

The XElement to search recursively.

tagName string

The tag name to match.

Returns

List<XElement>

GetElementsOfNameWithAttr(XElement, string, string, string)

Returns elements matching the specified tag name whose attribute has the exact specified value.

public static List<XElement> GetElementsOfNameWithAttr(XElement element, string tagName, string attributeName, string attributeValue)

Parameters

element XElement

The parent XElement to search.

tagName string

The tag name to match.

attributeName string

The attribute name to check.

attributeValue string

The expected attribute value.

Returns

List<XElement>

GetElementsOfNameWithAttrContains(XElement, string, string, string)

Returns elements matching the specified tag name whose attribute value contains the specified text.

public static IList<XElement> GetElementsOfNameWithAttrContains(XElement element, string tagName, string attributeName, string attributeValue)

Parameters

element XElement

The parent XElement to search.

tagName string

The tag name to match.

attributeName string

The attribute name to check.

attributeValue string

The text that the attribute value must contain.

Returns

IList<XElement>

GetElementsOfNameWithAttrWorker(XElement, string, string, string)

Worker method that finds elements by tag name whose attribute value contains the specified text.

public static List<XElement> GetElementsOfNameWithAttrWorker(XElement element, string tagName, string attributeName, string attributeValue)

Parameters

element XElement

The parent XElement to search.

tagName string

The tag name to match.

attributeName string

The attribute name to check.

attributeValue string

The text that the attribute value must contain.

Returns

List<XElement>

GetInnerXml(XElement)

Returns the inner XML content of an XElement.

public static string GetInnerXml(XElement parent)

Parameters

parent XElement

The parent XElement to read from.

Returns

string

GetValueOfElementOfNameOrSE(XElement, string)

Returns the trimmed value of a named element, or an empty string if not found.

public static string GetValueOfElementOfNameOrSE(XElement parentElement, string tagName)

Parameters

parentElement XElement

The XElement to search.

tagName string

The element name to find.

Returns

string

GetValueOfElementOfSecondLevelOrSE(XElement, string, string)

Returns the trimmed value of a second-level element, or an empty string if not found.

public static string GetValueOfElementOfSecondLevelOrSE(XElement parentElement, string firstLevelName, string secondLevelName)

Parameters

parentElement XElement

The root XElement to search from.

firstLevelName string

The first-level element name.

secondLevelName string

The second-level element name.

Returns

string

GetXml(XElement)

Serializes an XElement to its XML string representation.

public static string GetXml(XElement node)

Parameters

node XElement

The XElement to serialize.

Returns

string

InnerTextOfNode(XElement, string)

Returns the inner text of a descendant element with the specified name.

public static string InnerTextOfNode(XElement element, string elementName)

Parameters

element XElement

The parent XElement to search.

elementName string

The descendant element name to find.

Returns

string

IsRightTag(XElement, string)

Checks if the element matches the namespace-prefixed tag name.

public static bool IsRightTag(XElement element, string tagName)

Parameters

element XElement

The XElement to check.

tagName string

The namespace-prefixed tag name (e.g., "ns:tag").

Returns

bool

IsRightTag(XElement, string, string)

Checks if the element matches the specified local name and namespace.

public static bool IsRightTag(XElement element, string localName, string namespaceName)

Parameters

element XElement

The XElement to check.

localName string

The expected local name.

namespaceName string

The expected namespace name.

Returns

bool

IsRightTag(XName, string)

Checks if the XName matches the namespace-prefixed tag name by splitting into local name and namespace.

public static bool IsRightTag(XName xName, string tagName)

Parameters

xName XName

The XName to check.

tagName string

The namespace-prefixed tag name (e.g., "ns:tag").

Returns

bool

IsRightTag(XName, string, string)

Checks if the XName matches the specified local name and namespace.

public static bool IsRightTag(XName xName, string localName, string namespaceName)

Parameters

xName XName

The XName to check.

localName string

The expected local name.

namespaceName string

The expected namespace name.

Returns

bool

MakeAllElementsWithDefaultNs(XElement)

Re-creates the element with all descendants shifted into the default namespace.

public static XElement MakeAllElementsWithDefaultNs(XElement element)

Parameters

element XElement

The XElement to transform.

Returns

XElement

Minify(string)

Minifies XML by removing newlines, collapsing whitespace, and removing spaces between tags.

public static string Minify(string text)

Parameters

text string

The XML text to minify.

Returns

string

ReturnValueAllSubElementsSeparatedBy(XElement, string)

Returns the concatenated text values of all sub-elements separated by the specified delimiter, with XML tags replaced.

public static string ReturnValueAllSubElementsSeparatedBy(XElement element, string delimiter)

Parameters

element XElement

The XElement to extract values from.

delimiter string

The delimiter to insert between values.

Returns

string

XmlNamespaces(XmlNamespaceManager, bool)

Extracts namespace declarations from an XmlNamespaceManager into a dictionary, optionally prefixed with "xmlns:".

public static Dictionary<string, string> XmlNamespaces(XmlNamespaceManager namespaceManager, bool isWithPrefixedXmlnsColon)

Parameters

namespaceManager XmlNamespaceManager

The namespace manager to read from.

isWithPrefixedXmlnsColon bool

Whether to prefix keys with "xmlns:" (or "xmlns" for the default namespace).

Returns

Dictionary<string, string>