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
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
dictionaryDictionary<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
namespacePairsstring[]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
namespaceManagerXmlNamespaceManagerThe 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
Returns
CreateXDocument(string)
Creates an XDocument from an XML string or file path.
public static Task<XDocument> CreateXDocument(string contentOrFilePath)
Parameters
contentOrFilePathstringThe XML content string or file path.
Returns
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
pathOrContentstringThe file path or XML content string.
Returns
FormatXmlInMemory(string)
Formats XML content in memory using XDocument parsing. Returns the original string on parse failure.
public static string FormatXmlInMemory(string xml)
Parameters
xmlstringThe XML string to format.
Returns
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
nodeXContainerThe container to search.
tagNamestringThe tag name to find.
Returns
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
Returns
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
nodeXElementThe parent XElement to search.
tagNamestringThe tag name to match, optionally with namespace prefix (e.g., "ns:tag").
attributeNamestringThe attribute name to match.
attributeValuestringThe expected attribute value.
Returns
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
parentElementXElementThe root XElement to search from.
firstLevelNamestringThe first-level element name.
secondLevelNamestringThe second-level element name.
Returns
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
Returns
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
Returns
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
elementXElementThe parent XElement to search.
tagNamestringThe tag name to match.
attributeNamestringThe attribute name to check.
attributeValuestringThe expected attribute value.
Returns
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
elementXElementThe parent XElement to search.
tagNamestringThe tag name to match.
attributeNamestringThe attribute name to check.
attributeValuestringThe text that the attribute value must contain.
Returns
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
elementXElementThe parent XElement to search.
tagNamestringThe tag name to match.
attributeNamestringThe attribute name to check.
attributeValuestringThe text that the attribute value must contain.
Returns
GetInnerXml(XElement)
Returns the inner XML content of an XElement.
public static string GetInnerXml(XElement parent)
Parameters
parentXElementThe parent XElement to read from.
Returns
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
Returns
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
parentElementXElementThe root XElement to search from.
firstLevelNamestringThe first-level element name.
secondLevelNamestringThe second-level element name.
Returns
GetXml(XElement)
Serializes an XElement to its XML string representation.
public static string GetXml(XElement node)
Parameters
nodeXElementThe XElement to serialize.
Returns
InnerTextOfNode(XElement, string)
Returns the inner text of a descendant element with the specified name.
public static string InnerTextOfNode(XElement element, string elementName)
Parameters
elementXElementThe parent XElement to search.
elementNamestringThe descendant element name to find.
Returns
IsRightTag(XElement, string)
Checks if the element matches the namespace-prefixed tag name.
public static bool IsRightTag(XElement element, string tagName)
Parameters
elementXElementThe XElement to check.
tagNamestringThe namespace-prefixed tag name (e.g., "ns:tag").
Returns
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
elementXElementThe XElement to check.
localNamestringThe expected local name.
namespaceNamestringThe expected namespace name.
Returns
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
Returns
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
xNameXNameThe XName to check.
localNamestringThe expected local name.
namespaceNamestringThe expected namespace name.
Returns
MakeAllElementsWithDefaultNs(XElement)
Re-creates the element with all descendants shifted into the default namespace.
public static XElement MakeAllElementsWithDefaultNs(XElement element)
Parameters
elementXElementThe XElement to transform.
Returns
Minify(string)
Minifies XML by removing newlines, collapsing whitespace, and removing spaces between tags.
public static string Minify(string text)
Parameters
textstringThe XML text to minify.
Returns
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
elementXElementThe XElement to extract values from.
delimiterstringThe delimiter to insert between values.
Returns
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
namespaceManagerXmlNamespaceManagerThe namespace manager to read from.
isWithPrefixedXmlnsColonboolWhether to prefix keys with "xmlns:" (or "xmlns" for the default namespace).