From 40e00e0ab70e5a93661a0288860432c718d7ab4b Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 20 Apr 2020 16:09:20 -0400 Subject: [PATCH] feat: Adding methods for reading DateTimes from XML --- ICD.Common.Utils/Xml/XmlReaderExtensions.cs | 17 ++++++++++- ICD.Common.Utils/Xml/XmlUtils.cs | 34 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/ICD.Common.Utils/Xml/XmlReaderExtensions.cs b/ICD.Common.Utils/Xml/XmlReaderExtensions.cs index a017e53..1c19768 100644 --- a/ICD.Common.Utils/Xml/XmlReaderExtensions.cs +++ b/ICD.Common.Utils/Xml/XmlReaderExtensions.cs @@ -414,7 +414,7 @@ namespace ICD.Common.Utils.Xml } /// - /// Parses the element content as a short. + /// Parses the element content as a TimeSpan. /// /// /// @@ -439,6 +439,21 @@ namespace ICD.Common.Utils.Xml return IcdXmlConvert.ToTimeSpan(content); } + /// + /// Parses the element content as a DateTime. + /// + /// + /// + [PublicAPI] + public static DateTime ReadElementContentAsDateTime(this IcdXmlReader extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string content = extends.ReadElementContentAsString(); + return IcdXmlConvert.ToDateTime(content); + } + /// /// Parses the element content as an enum. /// diff --git a/ICD.Common.Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Xml/XmlUtils.cs index 2d26022..0a5777d 100644 --- a/ICD.Common.Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Xml/XmlUtils.cs @@ -481,12 +481,25 @@ namespace ICD.Common.Utils.Xml /// /// [PublicAPI] - public static TimeSpan? ReadChildElementContentAsTimeSpan(string xml, string childElement) + public static TimeSpan ReadChildElementContentAsTimeSpan(string xml, string childElement) { using (IcdXmlReader reader = GetChildElement(xml, childElement)) return reader.ReadElementContentAsTimeSpan(); } + /// + /// Gets the content of an immediate child. + /// + /// + /// + /// + [PublicAPI] + public static DateTime ReadChildElementContentAsDateTime(string xml, string childElement) + { + using (IcdXmlReader reader = GetChildElement(xml, childElement)) + return reader.ReadElementContentAsDateTime(); + } + /// /// Gets the content of an immediate child. /// @@ -711,6 +724,25 @@ namespace ICD.Common.Utils.Xml } } + /// + /// Gets the content of the immediate child. Returns null if the child element was not found. + /// + /// + /// + /// + [PublicAPI] + public static DateTime? TryReadChildElementContentAsDateTime(string xml, string childElement) + { + try + { + return ReadChildElementContentAsDateTime(xml, childElement); + } + catch (FormatException) + { + return null; + } + } + /// /// Gets the content of the immediate child. Returns default if the child element could not be parsed. ///