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
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
xmlDocumentXmlDocumentThe XML document to modify (passed by reference).
newRootElementNamestringThe name for the new root element.
attributesstring[]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
Returns
ChildNodes(XmlNode)
Returns child nodes as a generic list since XmlNodeList only implements IEnumerable.
public static List<XmlNode> ChildNodes(XmlNode node)
Parameters
nodeXmlNodeThe parent XML node.
Returns
CreateXmlDocument(string)
Creates and returns a new XmlDocument with whitespace preservation from the specified XML content.
public static XmlDocument CreateXmlDocument(string content)
Parameters
contentstringThe XML content string to load.
Returns
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
xmlContentstringThe XML content to format.
pathstringOptional file path for error context in exception messages.
Returns
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
Returns
GetAttributeWithName(XmlNode, string)
Finds the first attribute with the specified name on the given node.
public static XmlNode? GetAttributeWithName(XmlNode node, string attributeName)
Parameters
Returns
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
Returns
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
Returns
GetElementOfName(XmlNode, string)
Returns the first child element with the specified tag name.
public static XmlNode? GetElementOfName(XmlNode node, string tagName)
Parameters
Returns
GetElementsOfName(XmlNode, string)
Returns all child elements with the specified tag name.
public static IList<XmlNode> GetElementsOfName(XmlNode node, string tagName)
Parameters
Returns
GetInnerXml(XmlNode)
Returns the inner XML or CDATA value depending on the node type.
public static string GetInnerXml(XmlNode node)
Parameters
nodeXmlNodeThe XML node to read.
Returns
InnerTextOfNode(XmlNode)
Returns the inner text of the specified XML node.
public static string InnerTextOfNode(XmlNode node)
Parameters
nodeXmlNodeThe XML node to read.
Returns
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
textstringThe string to check.
Returns
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
xmlDocumentXmlDocumentThe XML document to modify (passed by reference).
newRootElementNamestringThe name for the new root element.
attributeNamesstring[]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
Returns
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)