-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLSerializationExample.cs
37 lines (35 loc) · 1.03 KB
/
XMLSerializationExample.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Lesson_12_1
{
[Serializable]
public class Person
{
public string? Name { get; set; }
public int Age { get; set; }
}
public class XMLSerializationExample
{
public static void SerializePerson(Person person, string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (StreamWriter writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, person);
}
}
public static Person DeserializePerson(string filePath)
{
XmlSerializer serializer = new XmlSerializer (typeof(Person));
using (StreamReader reader = new StreamReader(filePath))
{
return (Person)serializer.Deserialize(reader);
}
}
}
}