aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SQLExaminer/ExaminerSerializedObject.cs
blob: 7416ee29a5c5bcfb48bfe1753a9dddc69e23841d (plain)
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
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Tango.SQLExaminer
{
    public abstract class ExaminerSerializedObject<T> where T : class
    {
        public static T FromFile(String filePath)
        {
            return FromString(File.ReadAllText(filePath));
        }

        public static T FromString(String xml)
        {
            XmlSerializer f = new XmlSerializer(typeof(T), "");

            T config = null;

            using (var reader = new StringReader(xml))
            {
                config = f.Deserialize(reader) as T;
            }

            return config;
        }

        public void ToFile(String filePath)
        {
            File.WriteAllText(filePath, ToXml(), Encoding.Unicode);
        }

        public String ToXml()
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            XmlSerializer f = new XmlSerializer(typeof(T), "");

            String result = String.Empty;

            using (var st = new StringWriter())
            {
                f.Serialize(st, this, ns);
                result = st.ToString();
            }

            return result;
        }
    }
}