Class XmlUtil
This class contains utility methods for manipulating Xml elements
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public static class XmlUtil : Object
Methods
GetNodeText(XmlNode)
Get the text that is inside one Xml element node
Declaration
public static string GetNodeText(XmlNode node)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | The node |
Returns
Type | Description |
---|---|
String | Text, null will be returned if the node does not contain an XmlText childnode |
GetNodeText(XmlNode, String)
Get the text that is inside one Xml element node
Declaration
public static string GetNodeText(XmlNode node, string defaultValue)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | The node |
String | defaultValue | The string returned if the node is null |
Returns
Type | Description |
---|---|
String | Text, the default value will be returned if the node is null or does not contain an XmlText childnode |
GetValidXmlString(String)
Get rid of characters WriteElementString does not handle
Declaration
public static string GetValidXmlString(string text)
Parameters
Type | Name | Description |
---|---|---|
String | text | Text to verify |
Returns
Type | Description |
---|---|
String | Valid xml string |
InnerMerge(XmlNode, XmlNode)
Merge the inner XML's of two XML trees, i.e., leave the outer element alone (it is assumed to match), and merge the inner parts.
Declaration
public static string InnerMerge(XmlNode first, XmlNode second)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | first | First XML tree |
XmlNode | second | Second XML tree |
Returns
Type | Description |
---|---|
String | Markup of result, which does NOT contain the outer parts of any of the input elements |
Remarks
See the other Merge method for information on the merge process
Merge(XmlNode, XmlNode)
Merge two XML trees
Declaration
public static string Merge(XmlNode first, XmlNode second)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | first | First XML tree |
XmlNode | second | Second XML tree |
Returns
Type | Description |
---|---|
String | Markup of result |
Remarks
Equivalent nodes appear only once in the output. Two element nodes are treated equivalent if: their local names are equal, their namespace-uris are equal, and all their attributes are equal. Two text nodes are treated equal if their normalized content is equal. The same rule applies for comments and processing instructions.
See http://www.informatik.hu-berlin.de/~obecker/XSLT/ for a more detailed explanation!SafeGetAttribute(XmlNode, String)
Get an attribute text value; return string.Empty if it does not exist. This method does not throw any exceptions
Declaration
public static string SafeGetAttribute(XmlNode node, string attributeName)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | Node to get from |
String | attributeName | Name of attribute |
Returns
Type | Description |
---|---|
String | Value, or empty string |
SafeGetAttribute(XmlNode, String, String)
Get an attribute text value; return defaultValue if it does not exist or is empty. This method does not throw any exceptions
Declaration
public static string SafeGetAttribute(XmlNode node, string attributeName, string defaultValue)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | Node to get from |
String | attributeName | Name of attribute |
String | defaultValue | The value to return if attribute does not exist or is empty. |
Returns
Type | Description |
---|---|
String | Value, or defaultValue |
SafeGetAttributes(XmlNode, Boolean, String[])
Extract a number of attributes from a node, and return them as a dictionary
Declaration
public static Dictionary<string, string> SafeGetAttributes(XmlNode node, bool require, params string[] attributeNames)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | Node to extract data from |
Boolean | require | Require all attributes (exception if at least one is missing) - if false, missing attributes will be returned as blank values instead |
String[] | attributeNames | Array of case-sensitive attribute names |
Returns
Type | Description |
---|---|
Dictionary<String, String> | Dictionary containing one entry per requested attribute |
SafeGetRequiredAttribute(XmlNode, String)
Get an attribute text value; throw an exception if it is empty
Declaration
public static string SafeGetRequiredAttribute(XmlNode node, string attributeName)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | Node to get from |
String | attributeName | Name of attribute |
Returns
Type | Description |
---|---|
String | Value, or empty string |
Exceptions
Type | Condition |
---|---|
SoIllegalXmlException | Thrown if the attribute does not exist or has an empty value |
SafeRemoveAttribute(XmlNode, String)
Remove an attribute from a node, whether it exists or not ;-)
Declaration
public static void SafeRemoveAttribute(XmlNode node, string attrName)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | Node to be modified |
String | attrName | Attribute to remove |
SafeSetAttribute(XmlNode, String, String, XmlUtil.Override)
Set an attribute value; create the attribute or overwrite its previous value as appropriate
Declaration
public static void SafeSetAttribute(XmlNode node, string attrName, string attrValue, XmlUtil.Override overrideExisting)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | node | Node to modify |
String | attrName | Attribute name |
String | attrValue | Desired attribute value |
XmlUtil.Override | overrideExisting | Should an existing value be overwritten or not |
SafeSetChildNodeText(XmlNode, String, String)
Set the text value of the named child node. The child node is created, as an element, if it does not exist. Any previous value is lost! - so don't use this on child nodes that contain other nodes.
Declaration
public static XmlNode SafeSetChildNodeText(XmlNode parent, string childName, string childText)
Parameters
Type | Name | Description |
---|---|---|
XmlNode | parent | The parent node whose child is to be set |
String | childName | The name of the child node |
String | childText | The desired text value; null is acceptable and means 'do not create the inner text element'. |
Returns
Type | Description |
---|---|
XmlNode | The child node |