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
41 lines (39 loc) · 1.31 KB
/
XmlSerializableSerializer.cs
File metadata and controls
41 lines (39 loc) · 1.31 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
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL || NETSTANDARD1_1)
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using ServiceStack.Text;
namespace ServiceStack.Serialization
{
public partial class XmlSerializableSerializer : IStringSerializer
{
public static XmlSerializableSerializer Instance = new XmlSerializableSerializer();
public string SerializeToString<XmlDto>(XmlDto from)
{
try
{
using (var ms = MemoryStreamFactory.GetStream())
{
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($"Error serializing object of type {@from.GetType().FullName}", ex);
}
}
}
}
#endif