From e222ce424b35910da01842e499d77f41372a6472 Mon Sep 17 00:00:00 2001 From: Rashod Davis Date: Tue, 27 Nov 2018 16:46:26 -0500 Subject: [PATCH] feat: Adding short parsing methods to XML utils --- ICD.Common.Utils/Xml/XmlReaderExtensions.cs | 15 ++++++++++ ICD.Common.Utils/Xml/XmlUtils.cs | 32 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/ICD.Common.Utils/Xml/XmlReaderExtensions.cs b/ICD.Common.Utils/Xml/XmlReaderExtensions.cs index fcf1ae7..60d26c6 100644 --- a/ICD.Common.Utils/Xml/XmlReaderExtensions.cs +++ b/ICD.Common.Utils/Xml/XmlReaderExtensions.cs @@ -382,6 +382,21 @@ namespace ICD.Common.Utils.Xml return ushort.Parse(content); } + /// + /// Parses the element content as a short. + /// + /// + /// + [PublicAPI] + public static short ReadElementContentAsShort(this IcdXmlReader extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string content = extends.ReadElementContentAsString(); + return short.Parse(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 6caecd0..1119b0c 100644 --- a/ICD.Common.Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Xml/XmlUtils.cs @@ -406,6 +406,19 @@ namespace ICD.Common.Utils.Xml return reader.ReadElementContentAsUShort(); } + /// + /// Gets the content of an immediate child. + /// + /// + /// + /// + [PublicAPI] + public static short ReadChildElementContentAsShort(string xml, string childElement) + { + using (IcdXmlReader reader = GetChildElement(xml, childElement)) + return reader.ReadElementContentAsShort(); + } + /// /// Gets the content of an immediate child. /// @@ -566,6 +579,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 short? TryReadChildElementContentAsShort(string xml, string childElement) + { + try + { + return ReadChildElementContentAsShort(xml, childElement); + } + catch (FormatException) + { + return null; + } + } + /// /// Gets the content of the immediate child. Returns null if the child element was not found. ///