mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
feat: Added methods for serializing an XML array
This commit is contained in:
@@ -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 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 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 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
|
## [10.2.0] - 2019-12-04
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils.IO;
|
using ICD.Common.Utils.IO;
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
using Crestron.SimplSharp.CrestronXml;
|
using Crestron.SimplSharp.CrestronXml;
|
||||||
@@ -14,6 +15,8 @@ namespace ICD.Common.Utils.Xml
|
|||||||
{
|
{
|
||||||
public static class IcdXmlConvert
|
public static class IcdXmlConvert
|
||||||
{
|
{
|
||||||
|
#region Object Serialization
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Serializes the given instance to an xml string.
|
/// Serializes the given instance to an xml string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -114,6 +117,61 @@ namespace ICD.Common.Utils.Xml
|
|||||||
return converter.ReadXml(reader);
|
return converter.ReadXml(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Array Serialization
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Serializes the sequence to XML.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="childElementName"></param>
|
||||||
|
/// <param name="writer"></param>
|
||||||
|
/// <param name="items"></param>
|
||||||
|
/// <param name="elementName"></param>
|
||||||
|
public static void SerializeArray<T>(string elementName, string childElementName, [NotNull] IcdXmlTextWriter writer,
|
||||||
|
[NotNull] IEnumerable<T> 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Serializes the sequence to XML.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="childElementName"></param>
|
||||||
|
/// <param name="writer"></param>
|
||||||
|
/// <param name="items"></param>
|
||||||
|
/// <param name="serializeItem"></param>
|
||||||
|
/// <param name="elementName"></param>
|
||||||
|
public static void SerializeArray<T>(string elementName, string childElementName, [NotNull] IcdXmlTextWriter writer,
|
||||||
|
[NotNull] IEnumerable<T> items, [NotNull] Action<IcdXmlTextWriter, string, T> 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();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deserializes the child elements as items in an array.
|
/// Deserializes the child elements as items in an array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -162,7 +220,7 @@ namespace ICD.Common.Utils.Xml
|
|||||||
|
|
||||||
IXmlConverter converter = XmlConverterAttribute.GetConverterForType(type);
|
IXmlConverter converter = XmlConverterAttribute.GetConverterForType(type);
|
||||||
|
|
||||||
return DeserializeArray(type, reader, r => converter.ReadXml(r));
|
return DeserializeArray(type, reader, converter.ReadXml);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -209,6 +267,10 @@ namespace ICD.Common.Utils.Xml
|
|||||||
reader.Read();
|
reader.Read();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Value Serialization
|
||||||
|
|
||||||
public static string ToString(int value)
|
public static string ToString(int value)
|
||||||
{
|
{
|
||||||
return XmlConvert.ToString(value);
|
return XmlConvert.ToString(value);
|
||||||
@@ -431,5 +493,7 @@ namespace ICD.Common.Utils.Xml
|
|||||||
{
|
{
|
||||||
return XmlConvert.ToUInt64(data);
|
return XmlConvert.ToUInt64(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user