-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
62 lines (55 loc) · 2.27 KB
/
Program.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.IO;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializeApp
{
[Serializable]
public class Student
{
public string Name;
public string Kulliyyah;
}
internal class Program
{
public static void Main(string[] args)
{
var desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var maple = new Student() { Kulliyyah = "KOE", Name = "Fareez" };
Console.WriteLine("Select formatter (x = XML, b = Binary, j = JSON): " );
var chosenFormatter = Console.ReadLine();
switch (chosenFormatter?.ToLower())
{
case "x":
// Serialize to XML
var xml = new System.Xml.Serialization.XmlSerializer(typeof(Student));
var file = Path.Combine(desktopDir, "student.xml");
FileStream fs = new FileStream(file, FileMode.Create);
xml.Serialize(fs, maple);
fs.Close();
Console.WriteLine("Done serializing to XML. File saved to " + file);
break;
case "b":
// Serialize to Binary
BinaryFormatter bf = new BinaryFormatter();
var file1 = Path.Combine(desktopDir, "student.data");
FileStream fs1 = new FileStream(file1, FileMode.Create);
bf.Serialize(fs1, maple);
fs1.Close();
Console.WriteLine("Done serializing to Binary. File saved to " + file1);
break;
case "j":
// Serialize to JSON
var file2 = Path.Combine(desktopDir, "student.json");
var json = Newtonsoft.Json.JsonConvert.SerializeObject(maple);
File.WriteAllText(file2, json);
Console.WriteLine("Done serializing to JSON. File saved to " + file2);
break;
default:
Console.WriteLine("Invalid formatter");
Environment.Exit(1);
break;
}
}
}
}