Show / Hide Table of Contents

Class XmlUtil

This class contains utility methods for manipulating Xml elements

Inheritance
object
XmlUtil
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public static class XmlUtil

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, bool, params 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

bool 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, 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

© SuperOffice. All rights reserved.
SuperOffice |  Community |  Release Notes |  Privacy |  Site feedback |  Search Docs |  About Docs |  Contribute |  Back to top