diff --git a/CHANGELOG.md b/CHANGELOG.md index 23745c8..7eef7e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added enum extensions for finding the inclusion and exclusion of enum flags - Added DateTime extensions for adding years, months, days, hours, minutes and wrapping without modifying other values - Added shims for deserializing an XML array using a callback for each item + - Added methods for serializing an XML array ## [10.2.0] - 2019-12-04 ### Added diff --git a/ICD.Common.Utils/Xml/IcdXmlConvert.cs b/ICD.Common.Utils/Xml/IcdXmlConvert.cs index 7e97934..ac49839 100644 --- a/ICD.Common.Utils/Xml/IcdXmlConvert.cs +++ b/ICD.Common.Utils/Xml/IcdXmlConvert.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; +using ICD.Common.Properties; using ICD.Common.Utils.IO; #if SIMPLSHARP using Crestron.SimplSharp.CrestronXml; @@ -14,6 +15,8 @@ namespace ICD.Common.Utils.Xml { public static class IcdXmlConvert { + #region Object Serialization + /// /// Serializes the given instance to an xml string. /// @@ -114,6 +117,61 @@ namespace ICD.Common.Utils.Xml return converter.ReadXml(reader); } + #endregion + + #region Array Serialization + + /// + /// Serializes the sequence to XML. + /// + /// + /// + /// + /// + /// + public static void SerializeArray(string elementName, string childElementName, [NotNull] IcdXmlTextWriter writer, + [NotNull] IEnumerable items) + { + if (writer == null) + throw new ArgumentNullException("writer"); + + if (items == null) + throw new ArgumentNullException("items"); + + IXmlConverter converter = XmlConverterAttribute.GetConverterForType(typeof(T)); + + SerializeArray(elementName, childElementName, writer, items, (w, element, item) => converter.WriteXml(w, element, item)); + } + + /// + /// Serializes the sequence to XML. + /// + /// + /// + /// + /// + /// + /// + public static void SerializeArray(string elementName, string childElementName, [NotNull] IcdXmlTextWriter writer, + [NotNull] IEnumerable items, [NotNull] Action serializeItem) + { + if (writer == null) + throw new ArgumentNullException("writer"); + + if (items == null) + throw new ArgumentNullException("items"); + + if (serializeItem == null) + throw new ArgumentNullException("serializeItem"); + + writer.WriteStartElement(elementName); + { + foreach (T item in items) + serializeItem(writer, childElementName, item); + } + writer.WriteEndElement(); + } + /// /// Deserializes the child elements as items in an array. /// @@ -162,7 +220,7 @@ namespace ICD.Common.Utils.Xml IXmlConverter converter = XmlConverterAttribute.GetConverterForType(type); - return DeserializeArray(type, reader, r => converter.ReadXml(r)); + return DeserializeArray(type, reader, converter.ReadXml); } /// @@ -209,6 +267,10 @@ namespace ICD.Common.Utils.Xml reader.Read(); } + #endregion + + #region Value Serialization + public static string ToString(int value) { return XmlConvert.ToString(value); @@ -431,5 +493,7 @@ namespace ICD.Common.Utils.Xml { return XmlConvert.ToUInt64(data); } + + #endregion } }