feat: Adding methods for reading DateTimes from XML

This commit is contained in:
Chris Cameron
2020-04-20 16:09:20 -04:00
parent 64405a1cd6
commit 40e00e0ab7
2 changed files with 49 additions and 2 deletions

View File

@@ -414,7 +414,7 @@ namespace ICD.Common.Utils.Xml
}
/// <summary>
/// Parses the element content as a short.
/// Parses the element content as a TimeSpan.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
@@ -439,6 +439,21 @@ namespace ICD.Common.Utils.Xml
return IcdXmlConvert.ToTimeSpan(content);
}
/// <summary>
/// Parses the element content as a DateTime.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static DateTime ReadElementContentAsDateTime(this IcdXmlReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
string content = extends.ReadElementContentAsString();
return IcdXmlConvert.ToDateTime(content);
}
/// <summary>
/// Parses the element content as an enum.
/// </summary>

View File

@@ -481,12 +481,25 @@ namespace ICD.Common.Utils.Xml
/// <param name="childElement"></param>
/// <returns></returns>
[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();
}
/// <summary>
/// Gets the content of an immediate child.
/// </summary>
/// <param name="xml"></param>
/// <param name="childElement"></param>
/// <returns></returns>
[PublicAPI]
public static DateTime ReadChildElementContentAsDateTime(string xml, string childElement)
{
using (IcdXmlReader reader = GetChildElement(xml, childElement))
return reader.ReadElementContentAsDateTime();
}
/// <summary>
/// Gets the content of an immediate child.
/// </summary>
@@ -711,6 +724,25 @@ namespace ICD.Common.Utils.Xml
}
}
/// <summary>
/// Gets the content of the immediate child. Returns null if the child element was not found.
/// </summary>
/// <param name="xml"></param>
/// <param name="childElement"></param>
/// <returns></returns>
[PublicAPI]
public static DateTime? TryReadChildElementContentAsDateTime(string xml, string childElement)
{
try
{
return ReadChildElementContentAsDateTime(xml, childElement);
}
catch (FormatException)
{
return null;
}
}
/// <summary>
/// Gets the content of the immediate child. Returns default if the child element could not be parsed.
/// </summary>