feat: Implementing ReadXml for DefaultXmlConverter

This commit is contained in:
Chris Cameron
2018-10-17 17:13:44 -04:00
parent e9063682ef
commit d3d1dae2e1
2 changed files with 50 additions and 2 deletions

View File

@@ -48,7 +48,9 @@ namespace ICD.Common.Utils.Xml
/// </returns> /// </returns>
public override object ReadXml(IcdXmlReader reader) public override object ReadXml(IcdXmlReader reader)
{ {
throw new NotSupportedException(); string value = reader.ReadElementContentAsString();
return IcdXmlConvert.FromString(m_SerializeType, value);
} }
/// <summary> /// <summary>

View File

@@ -1,4 +1,6 @@
using System; using System;
using System.Globalization;
using System.Linq;
using System.Text; using System.Text;
using ICD.Common.Utils.IO; using ICD.Common.Utils.IO;
#if SIMPLSHARP #if SIMPLSHARP
@@ -52,7 +54,7 @@ namespace ICD.Common.Utils.Xml
/// <param name="type"></param> /// <param name="type"></param>
/// <param name="xml"></param> /// <param name="xml"></param>
/// <returns></returns> /// <returns></returns>
private static object DeserializeObject(Type type, string xml) public static object DeserializeObject(Type type, string xml)
{ {
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
@@ -182,5 +184,49 @@ namespace ICD.Common.Utils.Xml
return child.ToString(); return child.ToString();
} }
public static T FromString<T>(string value)
{
return (T)FromString(typeof(T), value);
}
public static object FromString(Type type, string value)
{
if (type == null)
throw new ArgumentNullException("type");
if (type == typeof(bool))
return bool.Parse(value);
if (type == typeof(byte))
return byte.Parse(value);
if (type == typeof(decimal))
return decimal.Parse(value);
if (type == typeof(char))
return value.Single();
if (type == typeof(double))
return double.Parse(value);
if (type == typeof(Guid))
return new Guid(value);
if (type == typeof(float))
return float.Parse(value);
if (type == typeof(int))
return int.Parse((value));
if (type == typeof(long))
return long.Parse(value);
if (type == typeof(sbyte))
return sbyte.Parse(value);
if (type == typeof(short))
return short.Parse(value);
if (type == typeof(TimeSpan))
return TimeSpan.Parse(value);
if (type == typeof(uint))
return uint.Parse(value);
if (type == typeof(ulong))
return ulong.Parse(value);
if (type == typeof(ushort))
return ushort.Parse(value);
return Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
} }
} }