728x90
    
    
  반응형
    
    
    
  
Xml 파일을 읽고 저장하기.
Class Serializer & Deserializer.
Xml 파일을 읽어 TreeView에 보여주기.

KpopList.xml
더보기
<?xml version="1.0"?>
<KPops xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <KPop>
    <Agency>YG</Agency>
    <Groups>
      <Group>
        <GroupName>BLACKPINK</GroupName>
        <Songs>
          <Song>Forever Young</Song>
          <Song>Don't Know What To Do</Song>
          <Song>Kick It</Song>
          <Song>Kill This Love</Song>
          <Song>Pink Venom</Song>
        </Songs>
      </Group>
      <Group>
        <GroupName>BabyMonster</GroupName>
        <Songs>
          <Song>SHEESH</Song>
          <Song>Stuck In The Middle</Song>
          <Song>BATTER UP</Song>
        </Songs>
      </Group>
    </Groups>
  </KPop>
  <KPop>
    <Agency>SM</Agency>
    <Groups>
      <Group>
        <GroupName>RIIZE</GroupName>
        <Songs>
          <Song>Siren</Song>
          <Song>Get A Guitar</Song>
        </Songs>
      </Group>
    </Groups>
  </KPop>
  <KPop>
    <Agency>JYP</Agency>
    <Groups>
      <Group>
        <GroupName>Stray Kids</GroupName>
        <Songs>
          <Song>S-Class</Song>
          <Song>CASE 143</Song>
          <Song>MANIAC</Song>
          <Song>Back Door</Song>
          <Song>Thunderous</Song>
        </Songs>
      </Group>
    </Groups>
  </KPop>
  <KPop>
    <Agency>ADOR</Agency>
    <Groups>
      <Group>
        <GroupName>NewJeans</GroupName>
        <Songs>
          <Song>Hype Boy</Song>
          <Song>GODS</Song>
          <Song>Bubble Gum</Song>
        </Songs>
      </Group>
    </Groups>
  </KPop>
</KPops>
| 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 97 98 99 100 101 102 103 104 105 106 107 | Imports System.IO Imports System.Xml Imports System.Xml.Serialization Public Class Form1     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load     End Sub     Public KpopList As List(Of KPop) = Nothing     <Serializable()>     Public Class KPop         Public Property Agency As String         <XmlArrayItem("Group")>         Public Property Groups As List(Of Group)         Public Sub New()             Groups = New List(Of Group)()         End Sub     End Class     Public Class Group         Public Property GroupName As String         <XmlArrayItem("Song")>         Public Property Songs As List(Of String)         Public Sub New()             Songs = New List(Of String)()         End Sub     End Class     Private Function ExportClass(ByVal filePath As String, ByVal kpops As List(Of KPop))         Dim serializer As New XmlSerializer(GetType(List(Of KPop)), New XmlRootAttribute("KPops"))         Using file As System.IO.FileStream = System.IO.File.Open(filePath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)             serializer.Serialize(file, kpops)         End Using     End Function     Private Function ImportClass(ByVal filePath As String) As List(Of KPop)         'Xml 데이터 KPop Class 형식으로 Deserialized         Dim serializer As New XmlSerializer(GetType(List(Of KPop)), New XmlRootAttribute("KPops"))         Using file = System.IO.File.OpenRead(filePath)             Return DirectCast(serializer.Deserialize(file), List(Of KPop))         End Using     End Function     Private Sub BtnOpen_Click(sender As Object, e As EventArgs) Handles BtnOpen.Click         Dim xmlDoc As New XmlDocument()         Using f As OpenFileDialog = New OpenFileDialog()             f.Filter = "(*.xml)|*.xml"             If f.ShowDialog() = DialogResult.OK Then                 Dim filepath = f.FileName                 TxtPath.Text = filepath                 'Xml 가져오기                 KpopList = ImportClass(filepath)                 'Xml 데이터 TreeView에 보여주기                 xmlDoc.Load(filepath)                 TreeView1.Nodes.Clear()                 TreeView1.Nodes.Add(New TreeNode(xmlDoc.DocumentElement.Name))                 Dim tNode As New TreeNode()                 tNode = TreeView1.Nodes(0)                 addTreeNode(xmlDoc.DocumentElement, tNode)                 TreeView1.ExpandAll()             End If         End Using     End Sub     Private Sub addTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode)         Dim xNode As XmlNode         Dim tNode As TreeNode         Dim xNodeList As XmlNodeList         If xmlNode.HasChildNodes Then             xNodeList = xmlNode.ChildNodes             For x As Integer = 0 To xNodeList.Count - 1                 xNode = xmlNode.ChildNodes(x)                 treeNode.Nodes.Add(New TreeNode(xNode.Name))                 tNode = treeNode.Nodes(x)                 addTreeNode(xNode, tNode)             Next         Else             treeNode.Text = xmlNode.OuterXml.Trim()         End If     End Sub     Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click         Dim f As SaveFileDialog = New SaveFileDialog()         f.Filter = "(*.xml)|*.xml"         If f.ShowDialog() = DialogResult.OK Then             TxtPath.Text = f.FileName             Dim di As DirectoryInfo = New DirectoryInfo(f.FileName)             If di.Exists Then di.Create()             'Xml 저장하기             ExportClass(f.FileName, KpopList)         End If     End Sub End Class | cs | 
728x90
    
    
  반응형
    
    
    
  'IT > DEV Study' 카테고리의 다른 글
| [Flutter] 플러터 윈도우 VSCode 개발환경 설정하기 (Flutter SDK 설치, Flutter Doctor 이슈 해결 방법) (7) | 2024.11.12 | 
|---|---|
| Log Scale 로그 스케일 공식 구간 나누는 방법 설명 (0) | 2024.10.14 | 
| [VB] Linq 람다식, Query 방식 vs. Function 방식 비교 (4) | 2024.04.18 | 
| LCR Meter 및 물리량 개념 간단 정리 (L, C, R, Z, X, G 등) (36) | 2024.03.20 | 
| Android Studio 안드로이드 스튜디오 설치 방법 (2023.08) (57) | 2023.08.29 |