-
Notifications
You must be signed in to change notification settings - Fork 2
/
Xml.cs
45 lines (36 loc) · 1.26 KB
/
Xml.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
43
44
45
using System;
using System.IO;
using System.Text;
using System.Text.Unicode;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace conversions
{
public class Xml : IRun
{
public string Name => nameof(Xml);
public async Task RunAsync<T>(T input)
{
// if you can, only create one serializer
// creating serializers is an expensive
// operation and can be slow
var serializer
= new XmlSerializer(typeof(T));
// we will write the our result to memory
await using var stream = new MemoryStream();
// the string will be utf8 encoded
await using var writer = new StreamWriter(stream, Encoding.UTF8);
// here we go!
serializer.Serialize(writer, input);
// flush our write to make sure its all there
await writer.FlushAsync();
// reset the stream back to 0
stream.Position = 0;
using var reader = new StreamReader(stream);
// we reread the stream to a string
var xml = await reader.ReadToEndAsync();
Console.Write($"{xml}\n");
}
}
}