[C# 문법] XML 파일 생성 및 읽는 방법


 

오늘은 XML 파일을 읽는 방법에 대해서 알려드리고자 해요!

 

실무에서 Database, FTP 설정파일을 보통은 ini파일이나 xml파일로 만들어서 관리하는데요!

 

지난번 ini 파일을 만들어서 읽는 방법은 설명을 드렸구요


INI 파일 생성 url - https://afsdzvcx123.tistory.com/108


 

오늘은 XML 파일을 만들어서 읽는 방법을 알려드리겠습니다.


 

1. 프로젝트 하나 생성


- text라는 프로젝트를 하나 생성 했습니다.


 

2. 생성된 프로젝트에 XML 파일 생성


[XML 파일 생성하는 방법]




생성한 프로젝트에서 마우스 우 클릭 -> XML파일 -> Config.xml 이름 설정 -> 추가 버튼을 눌러주시면 XML 파일이 아래와 같이 생성




위와 같이 Config.xml 파일이 생성 된 것을 확인

 



XML 파일 속성에 들어가셔서 출력 디렉터리에 복사를 항상 복사로 설정

 

3. 생성된 XML 파일 작성


1

2

3

4

5

6

7

8

9

10

<Configuration>

  <DATABASE>

    <ADDR>127.0.0.1</ADDR>

    <PORT>1521</PORT>

    <USER>WIN</USER>

    <PWD>@1234</PWD>

    <SERVICE_NAME>OCRL</SERVICE_NAME>

  </DATABASE

</Configuration>

 

Colored by Color Scripter

cs


(참고로 꼭 위와 같이  XML 작성하실 필요는 없습니다!)

 

4. test 프로젝트에서 XML 파일 읽는 소스코드 작성


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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Xml.Linq;

 

namespace test

{

    class Program

    {

        private static string configFileName = @"Config.xml";

 

        static void Main(string[] args)

        {

            string address = string.Empty;

            string port = string.Empty;

            string user = string.Empty;

            string passWord = string.Empty;

            string service = string.Empty;

 

            address = GetValue("DATABASE""ADDR");

            port = GetValue("DATABASE""PORT");

            user = GetValue("DATABASE""USER");

            passWord = GetValue("DATABASE""PWD");

            service = GetValue("DATABASE""SERVICE_NAME");

 

            string info = string.Format("Addr:{0}\n Port:{1}\n User:{2}\n PW:{3}\n Service:{4}\n"

                , address, port, user, passWord, service);

 

            Console.WriteLine(info);

 

        }

 

        public static string GetValue(params string[] args)

        {

            string result = string.Empty;

 

            try

            {

                XDocument xDoc = XDocument.Load(configFileName);

                result = GetNodeValue(xDoc.FirstNode as XElement, 0, args);

            }

            catch (Exception ex)

            {

                result = ex.Message;

                result = string.Empty;

            }

 

            return result;

        }

 

        private static string GetNodeValue(XElement node,

 int idx, params string[] args)

        {

            string result = string.Empty;

 

            if (args.Length > idx + 1)

                result = GetNodeValue(node.Element(args[idx]), ++idx, args);

            else

                result = node.Element(args[idx]).Value.ToString();

 

            return result;

        }

    }

}

 

Colored by Color Scripter

cs

 

실행 결과



위와 같이 XML에 쓰여있는 값들을 제대로 읽어온 것을 확인하실 수 있습니다^^

 


글 읽어 주셔서 감사합니다ㅎㅎ


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY