[c#] Serialization practice2




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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
 
namespace ConsoleApplication7
{
    [Serializable]
    public class Customer
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Pw { get; set; }
    }
 
    public class Serialize
    {
        public Serialize(string Path, Customer customer)
        {
            XmlSerializer serial = new XmlSerializer(typeof(Customer));
 
            StreamWriter sw = new StreamWriter(Path);
 
            serial.Serialize(sw, customer);
 
            //StreamWriter 닫기
            sw.Close();
        }
    }
 
    public class Deserialize
    {
        public Deserialize(string Path, ref Customer c)
        {
            XmlSerializer serial = new XmlSerializer(typeof(Customer));
 
            StreamReader sr = new StreamReader(Path);
            
            c = (Customer)serial.Deserialize(sr);
 
            //StreamReader 닫기
            sr.Close();
        }
    }
 
 
    class Program
    {
        static void Main(string[] args)
        {
            string strFileName = System.Environment.CurrentDirectory + "TestXml.xml";
 
            Customer c= new Customer(){Id="0001", Name="KIM", Pw="A12345"};
 
            Serialize mySerialize = new Serialize(strFileName, c);
 
            StreamReader sr = new StreamReader(strFileName);
 
            //한번에 읽기
            Console.WriteLine(sr.ReadToEnd());
 
            Console.WriteLine("********************************************************************");
 
            //StreamReader 의 맨처음으로 돌아가기
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
 
            //한줄씩 읽기
            while (!sr.EndOfStream)
            {
                Console.WriteLine(sr.ReadLine());
                
            }
            
            //StreamReader 닫기
            sr.Close();
 
            Console.WriteLine("********************************************************************");
 
            Customer cd = new Customer();
            Deserialize myDeserialize = new Deserialize(strFileName, ref cd);
 
            Console.WriteLine("ID:{0}, NAME:{1}, PW:{2}", cd.Id, cd.Name, cd.Pw);
 
            System.Console.ReadLine();
        }
    }
}
 
cs

댓글

이 블로그의 인기 게시물

[c#] DataTable을 dataGridView에 바인딩

[React] index.html 수정하기

[React] 프로젝트 생성