forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlSerializableSerializer.cs
More file actions
40 lines (38 loc) · 1.01 KB
/
XmlSerializableSerializer.cs
File metadata and controls
40 lines (38 loc) · 1.01 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
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using ServiceStack.DesignPatterns.Serialization;
using ServiceStack.ServiceModel.Support;
namespace ServiceStack.ServiceModel.Serialization
{
public class XmlSerializableSerializer : IStringSerializer
{
public static XmlSerializableSerializer Instance = new XmlSerializableSerializer();
public string Parse<XmlDto>(XmlDto from)
{
try
{
using (var ms = new MemoryStream())
{
using (XmlWriter xw = new XmlTextWriter(ms, Encoding.UTF8))
{
var ser = new XmlSerializerWrapper(from.GetType());
ser.WriteObject(xw, from);
xw.Flush();
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
}
}
}
}