forked from ServiceStackV3/ServiceStack.Contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataContractSerializer.cs
More file actions
53 lines (49 loc) · 1.36 KB
/
DataContractSerializer.cs
File metadata and controls
53 lines (49 loc) · 1.36 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace ServiceStack.ServiceClient.Web
{
public class DataContractSerializer
{
public static DataContractSerializer Instance = new DataContractSerializer();
public string Parse<XmlDto>(XmlDto from, bool indentXml)
{
try
{
using (var ms = new MemoryStream())
{
var dcs = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
dcs.WriteObject(ms, from);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
}
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
}
}
public string SerializeToString<XmlDto>(XmlDto from)
{
var sb = new StringBuilder();
try
{
using (var writer = new XmlTextWriter(new StringWriter(sb)))
{
var dcs = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
dcs.WriteObject(writer, from);
return sb.ToString();
}
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
}
}
public string Parse<XmlDto>(XmlDto from)
{
return Parse(from, false);
}
}
}