-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
42 lines (34 loc) · 1.03 KB
/
Extensions.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace SuperSocket.ServerManager.Client
{
public static class Extensions
{
public static string XmlSerialize(this object target)
{
var serializer = new XmlSerializer(target.GetType());
var stringBuilder = new StringBuilder();
using (var writer = new StringWriter(stringBuilder))
{
serializer.Serialize(writer, target);
writer.Close();
}
return stringBuilder.ToString();
}
public static T XmlDeserialize<T>(this string xml)
{
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(xml))
{
var target = serializer.Deserialize(reader);
if(target == null)
return default(T);
return (T)target;
}
}
}
}