3

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;
}
LorneCash
  • 1,446
  • 2
  • 16
  • 30

1 Answers1

0

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.

mythz
  • 141,670
  • 29
  • 246
  • 390