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
node
XmlElementXML node to add attributes to.
attributes
IDictionaryAttributes to be added.
removeEmptyEntries
boolIf 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
XmlElementParent XML element.
values
IDictionaryCollection of element values.
elementName
stringIf 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
boolIf true, only elements with non-empty value are added to the parent element
node
.
Exceptions
- ArgumentException
elementName
was not supplied and some keys ofvalues
are an empty string.- ArgumentNullException
node
is null.- InvalidOperationException
Keys of
values
cannot be ordered ornode
is missing owner document.- XmlException
elementName
was not supplied and some keys ofvalues
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
XmlElementParent XML node
text
stringInput 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
XmlDocumentXML document
omitXmlDeclaration
boolIf 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
XmlDocumentXML document
stream
StreamThe stream to write to.
omitXmlDeclaration
boolIf true, the XML declaration is not included into the output.
encoding
EncodingEncoding to be used when writing the content to stream.
Remarks
Ensures indentation and line breaking after each element.
Exceptions
- ArgumentNullException
document
is null.