mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
feat: Implementing ReadXml for DefaultXmlConverter
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user