From 7792b016294ec482364ea93f954dac79b8a19b28 Mon Sep 17 00:00:00 2001 From: Austin Noska Date: Fri, 31 May 2019 15:52:47 -0400 Subject: [PATCH] feat: Added Shim to read a list from xml with no root element --- ICD.Common.Utils/Xml/XmlUtils.cs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/ICD.Common.Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Xml/XmlUtils.cs index 9ad62a8..9cf03a8 100644 --- a/ICD.Common.Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Xml/XmlUtils.cs @@ -972,6 +972,22 @@ namespace ICD.Common.Utils.Xml return ReadDictFromXml(xml, rootElement, childElement, keyElement, valueElement, readKey, readValue); } + /// + /// Calls childElementCallback for each item in the list. + /// + /// + /// + /// + /// + public static IEnumerable ReadListFromXml(string xml, string childElement, Func readChild) + { + if (readChild == null) + throw new ArgumentNullException("readChild"); + + foreach (string child in GetChildElementsAsString(xml, childElement)) + yield return readChild(child); + } + /// /// Calls childElementCallback for each item in the list. /// @@ -988,10 +1004,22 @@ namespace ICD.Common.Utils.Xml if (!TryGetChildElementAsString(xml, rootElement, out xml)) yield break; - foreach (string child in GetChildElementsAsString(xml, childElement)) - yield return readChild(child); + foreach (T child in ReadListFromXml(xml, childElement, readChild)) + yield return child; } + /// + /// Deserializes the xml to a list. + /// + /// + /// + /// + public static IEnumerable ReadListFromXml(string xml, string childElement) + { + Func readChild = IcdXmlConvert.DeserializeObject; + + return ReadListFromXml(xml, childElement, readChild); + } /// /// Deserializes the xml to a list.