1 |
6b4c34a2
|
Eliška Mourycová
|
using System;
|
2 |
|
|
using System.Collections.Generic;
|
3 |
|
|
using System.IO;
|
4 |
|
|
using System.Linq;
|
5 |
|
|
using System.Text;
|
6 |
|
|
using System.Threading.Tasks;
|
7 |
|
|
using System.Xml.Serialization;
|
8 |
|
|
|
9 |
|
|
namespace ServerApp.Connection.XMLProtocolHandler
|
10 |
|
|
{
|
11 |
|
|
public class XmlCommunication
|
12 |
|
|
{
|
13 |
|
|
public static string Serialize<T>(T toSerialize)
|
14 |
|
|
{
|
15 |
|
|
var xml = new XmlSerializer(toSerialize.GetType());
|
16 |
|
|
|
17 |
|
|
using (var stream = new StringWriter())
|
18 |
|
|
{
|
19 |
|
|
xml.Serialize(stream, toSerialize);
|
20 |
|
|
return stream.ToString();
|
21 |
|
|
}
|
22 |
|
|
}
|
23 |
|
|
|
24 |
5cb4249a
|
Eliška Mourycová
|
//public static T Deserialize<T>(T toDeserialize, string xmlString)
|
25 |
|
|
//{
|
26 |
|
|
// using (var stream = new StringReader(xmlString))
|
27 |
|
|
// {
|
28 |
|
|
// var xml = new XmlSerializer(toDeserialize.GetType());
|
29 |
|
|
// T deserialized = (T)xml.Deserialize(stream);
|
30 |
|
|
// return deserialized;
|
31 |
|
|
// }
|
32 |
|
|
//}
|
33 |
|
|
|
34 |
|
|
public static T Deserialize<T>(string xmlString)
|
35 |
6b4c34a2
|
Eliška Mourycová
|
{
|
36 |
|
|
using (var stream = new StringReader(xmlString))
|
37 |
|
|
{
|
38 |
5cb4249a
|
Eliška Mourycová
|
var xml = new XmlSerializer(typeof(T));
|
39 |
6b4c34a2
|
Eliška Mourycová
|
T deserialized = (T)xml.Deserialize(stream);
|
40 |
|
|
return deserialized;
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
5cb4249a
|
Eliška Mourycová
|
|
44 |
6b4c34a2
|
Eliška Mourycová
|
}
|
45 |
|
|
}
|