본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# (C Sharp) - XML 파일 저장하기 ( 쓰기 ) / 읽어오기 ( 읽기 ) / LINQ

728x90
반응형

【 WPF & C# (C Sharp) 】

# XML 파일 저장하기 ( 쓰기 ) / 읽어오기 ( 읽기 ) / LINQ


 @ MainWindow.xaml    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Window x:Class="WpfApp24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp24"
        mc:Ignorable="d"
        Title="MainWindow" Height="268.627" Width="204.412">
    <Grid>
        <Button Content="쓰기" HorizontalAlignment="Left" Margin="10,13,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <Button Content="읽기" HorizontalAlignment="Left" Margin="103,13,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="189" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="168"/>
    </Grid>
</Window>
 
cs



 @ MainWindow.xaml.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
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
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
 
namespace WpfApp24
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string url = @".\Emp.xml";
            XDocument xdoc = new XDocument(new XDeclaration("1.0""UTF-8"null));
            XElement xroot = new XElement("Employees");
            xdoc.Add(xroot);
 
            XElement xe1 = new XElement("Employee",
                new XAttribute("Id""1001"),
                new XElement("Name""Tim"),
                new XElement("Dept""Sales")
            );
 
            XElement xe2 = new XElement("Employee",
                new XAttribute("Id""1002"),
                new XElement("Name""John"),
                new XElement("Dept""HR")
            );
 
            xroot.Add(xe1);
            xroot.Add(xe2);
 
            xdoc.Save(url);
 
            /* 출력파일  Emp.xml
            <?xml version=="1.0" encoding="utf--166"?>
            <Employees>
              <Employee Id="1001">
                <!---Employee#10011--->
                <<Name>Tim</Name>
                <Dept>Sales</Dept>
              </Employee>
              <Employee Id=="1002">
                <Name>John</Name>
                <Dept>HR</Dept>
              </Employee>
            </Employees>
            */
 
        }
 
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string url = @".\Emp.xml";
 
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(url);
                XmlNodeList xnList = xml.SelectNodes("Employees/Employee"); //접근할 노드
                foreach (XmlNode xn in xnList)
                {
                    string part1 = xn["Name"].InnerText;
                    string part2 = xn["Dept"].InnerText;
                    textbox1.AppendText(part1 + " | " + part2+ "\n");
                }
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show("XML 문제 발생\r\n" + ex);
            }
        }
    }
}
 
cs



@ Memo

 

using 부분에 using System.Xml; 하고 using System.Xml.Linq; 를 넣어줘야 정상 작동 된다.


내가 만든것이 아니고 다른사람이 만든 소스다.
넘넘 간결하고 편리하다. 아래 사이트를 보며 공부했다.


728x90
반응형