forked from ServiceStackV3/ServiceStack.Contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataContractDeserializer.cs
More file actions
37 lines (33 loc) · 899 Bytes
/
Copy pathDataContractDeserializer.cs
File metadata and controls
37 lines (33 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
namespace ServiceStack.ServiceClient.Web
{
public class DataContractDeserializer
{
public static DataContractDeserializer Instance = new DataContractDeserializer();
public To Parse<To>(string xml)
{
var type = typeof(To);
return (To)Parse(xml, type);
}
public object Parse(string xml, Type type)
{
try
{
if (string.IsNullOrEmpty(xml)) throw new ArgumentNullException("xml");
var bytes = Encoding.UTF8.GetBytes(xml);
using (var ms = new MemoryStream(bytes))
{
var dcs = new System.Runtime.Serialization.DataContractSerializer(type);
return dcs.ReadObject(ms);
}
}
catch (Exception ex)
{
throw new SerializationException("DeserializeDataContract: Error converting type: " + ex.Message, ex);
}
}
}
}