Reputation: 1604
My current method for deserialization looks like this:
public static object Deserialize(string xml, Type toType)
{
object result = null;
using (var stream = new MemoryStream())
{
var data = Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
var deserializer = new DataContractSerializer(toType);
result = deserializer.ReadObject(stream);
}
return result;
}
Can I use ServiceStack's deserialization here somehow? Maybe I should be using something other than the FromXml method? The important part to me is that I need to be able to pass in the System.Type
parameter (toType) but have the result be of type object
.
This obviously doesn't work:
public static object Deserialize(string xml, Type toType)
{
object result = null;
result = xml.FromXml<toType>();
return result;
}
Neither does this:
public static object Deserialize(string xml, Type toType)
{
object result = null;
result = xml.FromXml<object>();
return result;
}
Upvotes: 3
Views: 197
Reputation: 143319
The API to deserialize in ServiceStack.Text is:
var dto = XmlSerializer.Deserialize(xml, toType);
But for XML it's just a wrapper around .NET's Xml DataContractSerializer so it's not much of a benefit.
Upvotes: 0