Class XmlExtensions
Extension methods for System.Xml classes.
public static class XmlExtensions
- Inheritance
-
objectXmlExtensions
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
nodeXmlElementXML node to add attributes to.
attributesIDictionaryAttributes to be added.
removeEmptyEntriesboolIf true, only attributes with non-empty value are added to the element.
Exceptions
- ArgumentException
Some key of
attributesis an empty string.- ArgumentNullException
nodeis null.- InvalidOperationException
Keys of
attributescannot be ordered.- XmlException
Some key of
attributescontains 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
nodeXmlElementParent XML element.
valuesIDictionaryCollection of element values.
elementNamestringIf 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.
transformAction<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.removeEmptyEntriesboolIf true, only elements with non-empty value are added to the parent element
node.
Exceptions
- ArgumentException
elementNamewas not supplied and some keys ofvaluesare an empty string.- ArgumentNullException
nodeis null.- InvalidOperationException
Keys of
valuescannot be ordered ornodeis missing owner document.- XmlException
elementNamewas not supplied and some keys ofvaluescontain 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
nodeXmlElementParent XML node
textstringInput text
Exceptions
- ArgumentNullException
nodeis null.- ArgumentNullException
textis null.
ToFormattedXmlString(XmlDocument, bool)
Returns formatted XML content.
public static string ToFormattedXmlString(this XmlDocument document, bool omitXmlDeclaration = false)
Parameters
documentXmlDocumentXML document
omitXmlDeclarationboolIf true, the XML declaration is not included into the output
Returns
- string
Remarks
Ensures indentation and line breaking after each element.
Exceptions
- ArgumentNullException
documentis 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
documentXmlDocumentXML document
streamStreamThe stream to write to.
omitXmlDeclarationboolIf true, the XML declaration is not included into the output.
encodingEncodingEncoding to be used when writing the content to stream.
Remarks
Ensures indentation and line breaking after each element.
Exceptions
- ArgumentNullException
documentis null.