Table of Contents

Class XmlExtensions

Namespace
CMS.Helpers
Assembly
CMS.Helpers.dll

Extension methods for System.Xml classes.

public static class XmlExtensions
Inheritance
object
XmlExtensions

Methods

AddAttributes(XmlElement, IDictionary, bool)

Adds the given list of attributes to the XML node. Adds the attributes in alphabetic order to maintain stability of order.

public static void AddAttributes(this XmlElement node, IDictionary attributes, bool removeEmptyEntries = true)

Parameters

node XmlElement

XML node to add attributes to.

attributes IDictionary

Attributes to be added.

removeEmptyEntries bool

If true, only attributes with non-empty value are added to the element.

Exceptions

ArgumentException

Some key of attributes is an empty string.

ArgumentNullException

node is null.

InvalidOperationException

Keys of attributes cannot be ordered.

XmlException

Some key of attributes contains characters not suitable for an XML attribute name.

AddChildElements(XmlElement, IDictionary, string, Action<XmlElement, string, object>, bool)

Adds the collection of values into the XML element, each value as a nested element. Ensures the alphabetic order of the values in the element.

public static void AddChildElements(this XmlElement node, IDictionary values, string elementName = null, Action<XmlElement, string, object> transform = null, bool removeEmptyEntries = true)

Parameters

node XmlElement

Parent XML element.

values IDictionary

Collection of element values.

elementName string

If set, the elements are added with the given name and the key is stored in attribute name. Key is used for the element name otherwise.

transform Action<XmlElement, string, object>

Function allowing to modify the added element that has three parameters - XML element to modify, string key and value from the collection values.

removeEmptyEntries bool

If true, only elements with non-empty value are added to the parent element node.

Exceptions

ArgumentException

elementName was not supplied and some keys of values are an empty string.

ArgumentNullException

node is null.

InvalidOperationException

Keys of values cannot be ordered or node is missing owner document.

XmlException

elementName was not supplied and some keys of values contain characters not suitable for an XML element name.

AppendCData(XmlElement, string)

Appends CDATA sections to the XML element based on the input text. If the text already contains CDATA tags, the text is split into corresponding number of CDATA sections in place of the CDATA end tags.

public static void AppendCData(this XmlElement node, string text)

Parameters

node XmlElement

Parent XML node

text string

Input text

Exceptions

ArgumentNullException

node is null.

ArgumentNullException

text is null.

ToFormattedXmlString(XmlDocument, bool)

Returns formatted XML content.

public static string ToFormattedXmlString(this XmlDocument document, bool omitXmlDeclaration = false)

Parameters

document XmlDocument

XML document

omitXmlDeclaration bool

If true, the XML declaration is not included into the output

Returns

string

Remarks

Ensures indentation and line breaking after each element.

Exceptions

ArgumentNullException

document is null.

WriteFormattedXmlToStream(XmlDocument, Stream, bool, Encoding)

Writes formatted XML content to given stream.

public static void WriteFormattedXmlToStream(this XmlDocument document, Stream stream, bool omitXmlDeclaration = false, Encoding encoding = null)

Parameters

document XmlDocument

XML document

stream Stream

The stream to write to.

omitXmlDeclaration bool

If true, the XML declaration is not included into the output.

encoding Encoding

Encoding to be used when writing the content to stream.

Remarks

Ensures indentation and line breaking after each element.

Exceptions

ArgumentNullException

document is null.