refactor: Exposing missing XmlConvert methods

This commit is contained in:
Chris Cameron
2019-05-14 16:15:48 -04:00
parent 9ad49540d2
commit 4c93515f83
3 changed files with 156 additions and 20 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using ICD.Common.Utils.IO;
#if SIMPLSHARP
@@ -213,37 +212,127 @@ namespace ICD.Common.Utils.Xml
throw new ArgumentNullException("type");
if (type == typeof(bool))
return bool.Parse(value);
return ToBool(value);
if (type == typeof(byte))
return byte.Parse(value);
return ToByte(value);
if (type == typeof(decimal))
return decimal.Parse(value);
return ToDecimal(value);
if (type == typeof(char))
return value.Single();
return ToChar(value);
if (type == typeof(double))
return double.Parse(value);
return ToDouble(value);
if (type == typeof(Guid))
return new Guid(value);
return ToGuid(value);
if (type == typeof(float))
return float.Parse(value);
return ToSingle(value);
if (type == typeof(int))
return int.Parse((value));
return ToInt32(value);
if (type == typeof(long))
return long.Parse(value);
return ToInt64(value);
if (type == typeof(sbyte))
return sbyte.Parse(value);
return ToSByte(value);
if (type == typeof(short))
return short.Parse(value);
return ToInt16(value);
if (type == typeof(TimeSpan))
return TimeSpan.Parse(value);
return ToTimeSpan(value);
if (type == typeof(uint))
return uint.Parse(value);
return ToUInt32(value);
if (type == typeof(ulong))
return ulong.Parse(value);
return ToUInt64(value);
if (type == typeof(ushort))
return ushort.Parse(value);
return ToUInt16(value);
return Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
public static bool ToBool(string data)
{
return XmlConvert.ToBoolean(data);
}
public static byte ToByte(string data)
{
return XmlConvert.ToByte(data);
}
public static char ToChar(string data)
{
return XmlConvert.ToChar(data);
}
public static DateTime ToDateTime(string data, string format)
{
return XmlConvert.ToDateTime(data, format);
}
public static DateTime ToDateTime(string data, string[] formats)
{
return XmlConvert.ToDateTime(data, formats);
}
public static DateTime ToDateTime(string data, XmlDateTimeSerializationMode dateTimeOption)
{
return XmlConvert.ToDateTime(data, dateTimeOption);
}
public static decimal ToDecimal(string data)
{
return XmlConvert.ToDecimal(data);
}
public static double ToDouble(string data)
{
return XmlConvert.ToDouble(data);
}
public static Guid ToGuid(string data)
{
return XmlConvert.ToGuid(data);
}
public static short ToInt16(string data)
{
return XmlConvert.ToInt16(data);
}
public static int ToInt32(string data)
{
return XmlConvert.ToInt32(data);
}
public static long ToInt64(string data)
{
return XmlConvert.ToInt64(data);
}
public static sbyte ToSByte(string data)
{
return XmlConvert.ToSByte(data);
}
public static float ToSingle(string data)
{
return XmlConvert.ToSingle(data);
}
public static TimeSpan ToTimeSpan(string data)
{
return XmlConvert.ToTimeSpan(data);
}
public static ushort ToUInt16(string data)
{
return XmlConvert.ToUInt16(data);
}
public static uint ToUInt32(string data)
{
return XmlConvert.ToUInt32(data);
}
public static ulong ToUInt64(string data)
{
return XmlConvert.ToUInt64(data);
}
}
}

View File

@@ -349,7 +349,7 @@ namespace ICD.Common.Utils.Xml
throw new ArgumentNullException("extends");
string content = extends.ReadElementContentAsString();
return uint.Parse(content);
return IcdXmlConvert.ToUInt32(content);
}
/// <summary>
@@ -364,7 +364,7 @@ namespace ICD.Common.Utils.Xml
throw new ArgumentNullException("extends");
string content = extends.ReadElementContentAsString();
return int.Parse(content);
return IcdXmlConvert.ToInt32(content);
}
/// <summary>
@@ -379,7 +379,7 @@ namespace ICD.Common.Utils.Xml
throw new ArgumentNullException("extends");
string content = extends.ReadElementContentAsString();
return ushort.Parse(content);
return IcdXmlConvert.ToUInt16(content);
}
/// <summary>
@@ -394,7 +394,22 @@ namespace ICD.Common.Utils.Xml
throw new ArgumentNullException("extends");
string content = extends.ReadElementContentAsString();
return short.Parse(content);
return IcdXmlConvert.ToInt16(content);
}
/// <summary>
/// Parses the element content as a short.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static TimeSpan ReadElementContentAsTimeSpan(this IcdXmlReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
string content = extends.ReadElementContentAsString();
return IcdXmlConvert.ToTimeSpan(content);
}
/// <summary>

View File

@@ -458,6 +458,19 @@ namespace ICD.Common.Utils.Xml
return reader.ReadElementContentAsByte();
}
/// <summary>
/// Gets the content of an immediate child.
/// </summary>
/// <param name="xml"></param>
/// <param name="childElement"></param>
/// <returns></returns>
[PublicAPI]
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>
@@ -663,6 +676,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 TimeSpan? TryReadChildElementContentAsTimeSpan(string xml, string childElement)
{
try
{
return ReadChildElementContentAsTimeSpan(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>