Converting from String to <T>
I would suggest instead of trying to parse XML yourself, you try to create classes that would deserialize from the XML into the classes. I would strongly recommend following bendewey's answer.
But if you cannot do this, there is hope. You can use Convert.ChangeType
.
public static T GetValue<T>(String value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
And use like so
GetValue<int>("12"); // = 12
GetValue<DateTime>("12/12/98");
You can start with something roughly like this:
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
if (converter != null)
{
return (T)converter.ConvertFrom(value);
}
If you have to parse attributes that are special types, like colors or culture strings or whatnot, you will of course have to build special cases into the above. But this will handle most of your primitive types.