본문 바로가기

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

xml parsing ( 파싱 )

728x90
반응형


xml parsing ( 파싱 )



testXML.zip




MainWindow.xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<Window x:Class="testXML.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:testXML"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="btn" Content="읽기" HorizontalAlignment="Left" Margin="10,237,0,0" VerticalAlignment="Top" Width="75" Click="btn_Click"/>
        <ListBox x:Name="lBox1" HorizontalAlignment="Left" Height="197" Margin="10,10,0,0" VerticalAlignment="Top" Width="173"/>
        <Button x:Name="btn_fOpen" Content="XML 열기" HorizontalAlignment="Left" Margin="10,212,0,0" VerticalAlignment="Top" Width="75" Click="btn_fOpen_Click"/>
        <Label x:Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="90,212,0,0" VerticalAlignment="Top"/>
        <ListBox x:Name="lBox2" HorizontalAlignment="Left" Height="197" Margin="188,10,0,0" VerticalAlignment="Top" Width="173"/>
 
    </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
using Microsoft.Win32;
using System;
using System.Windows;
using System.Xml;
 
namespace testXML
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void btn_fOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "";   // 기본 파일명
            dlg.DefaultExt = "";   // 기본 확장자
            dlg.Filter = "XML파일|*.xml"// 확장자 필터
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true) lbl.Content = dlg.FileName;
        }
 
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            string url = lbl.Content.ToString();
            try
            {
                // xml 문서 구조
                // [xmldoc 문서]
                //    [root] > [nodelist] (노드묶음) > [node] > [XAttribute] ex) node id=1  > node.Inner.Text
                
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(url); // string 형태를 읽을때 xmldoc.LoadXml(url)
            XmlElement root = xmldoc.DocumentElement;
 
            // 노드 요소들
            XmlNodeList xnList = root.ChildNodes;
                
                // node 값 읽기_1
                lBox1.Items.Clear();
                foreach (XmlNode node in xnList)
                {
                    string nodeStr;
                    nodeStr = "+ " + node.Name + " Id = "+ node.Attributes["Id"].Value;
 
                    foreach (XmlNode node_ChildNode in node.ChildNodes)
                    {
                        nodeStr += "\n - " + node_ChildNode.Name;
                    }
                    lBox1.Items.Add(nodeStr);
                }
 
                // node 값 읽기_2
                lBox2.Items.Clear();
                foreach (XmlNode node in xnList)
                {
                    switch (node.Name)
                    {
                        case "node1":
                        string a = node["Name"].InnerText;
                        string b = node["Dept"].InnerText;
                            lBox2.Items.Add("node1 \n" + a + "\n" + b);
                            continue;  // 계속해서
 
                    case "node2":
                        string c = node.InnerText;
                            lBox2.Items.Add("node2 \n" + c);
                            break;
 
                    case "root_c":
                        int d = int.Parse(node.InnerText);
                        break;
                    }
                }
            }
 
            catch (ArgumentException ex)
            {
            MessageBox.Show(ex.ToString());
            }
        }
    }
}
 
cs




언제나 그렇듯이...

독학은... 이해를 하기까지는 오래걸린다.

그래도 이해하기 전에 포기하면 평생 이해할 수 없다.

그러나... 이해하고나면 저거를 이렇게 오래걸렸나 싶다.

독학은 참 힘들다. ㅠㅠ




[참고자료]

https://blog.naver.com/qkrqkr21/220762636205


[관련자료]

XML 만들기

https://rintiantta.blog.me/40114722075


https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/concepts/linq/creating-xml-trees-linq-to-xml-2



728x90
반응형