반응형

* VBNET XML File Write & Read 예제...

 

메인화면

전체 소스 코드

Form1.vb

 

Public Class Form1

    Dim strLocal As String = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\"))
    Dim strXMLFile As String = "\XMLText.xml"

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        'XMLFile Write

        '파일이 존재 하면은... 삭제하고 다시...
        If System.IO.File.Exists(strLocal + strXMLFile) Then
            System.IO.File.Delete(strLocal + strXMLFile)
        End If

        'XML Create
        Dim xdDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        '최상위 노드 생성...
        Dim xnRoot As System.Xml.XmlNode = xdDoc.CreateElement("테스트")

        'xmRoot 에 속한 하위 노드...
        Dim xnTmp As System.Xml.XmlNode = xdDoc.CreateElement("XMLFileEx")
        xnTmp.InnerText = "XML Test"

        '최상위 노드에 하위노드 xmTmp 추가... 
        xnRoot.AppendChild(xnTmp)

        'CheckBox 저장부...
        Dim xnCheckBox As System.Xml.XmlNode = xdDoc.CreateElement("ProgramCheckBox")
        Dim xaCheckBox1 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("CheckBox1")
        Dim xaCheckBox2 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("CheckBox2")
        Dim xaCheckBox3 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("CheckBox3")

        '값 대입...
        xaCheckBox1.Value = checkBox1.Checked.ToString()
        xaCheckBox2.Value = checkBox2.Checked.ToString()
        xaCheckBox3.Value = checkBox3.Checked.ToString()

        'xnCheckBox 노드의 속성으로 추가...
        xnCheckBox.Attributes.Append(xaCheckBox1)
        xnCheckBox.Attributes.Append(xaCheckBox2)
        xnCheckBox.Attributes.Append(xaCheckBox3)

        '최상위 노드의 하위 노드로 추가 
        xnRoot.AppendChild(xnCheckBox)

        'Radio 저장부...
        Dim xnRadio As System.Xml.XmlNode = xdDoc.CreateElement("ProgramRadio")
        Dim xaRadio1 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("Radio1")
        Dim xaRadio2 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("Radio2")
        Dim xaRadio3 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("Radio3")

        '값 대입...
        xaRadio1.Value = radioButton1.Checked.ToString()
        xaRadio2.Value = radioButton2.Checked.ToString()
        xaRadio3.Value = radioButton3.Checked.ToString()

        'xnRadio 노드의 속성으로 추가...
        xnRadio.Attributes.Append(xaRadio1)
        xnRadio.Attributes.Append(xaRadio2)
        xnRadio.Attributes.Append(xaRadio3)

        '최상위 노드의 하위 노드로 추가
        xnRoot.AppendChild(xnRadio)

        'XML File 로 저장
        xdDoc.AppendChild(xnRoot)
        xdDoc.Save(strLocal + strXMLFile)

    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        'XMLFile Read

        '파일이 존재 하지 않으면...
        If Not System.IO.File.Exists(strLocal + strXMLFile) Then
            Return
        End If

        textBox1.Text = ""
        Dim xdDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        'XML File Load
        xdDoc.Load(strLocal + strXMLFile)

        '최상위 노드 1개에 하위 노드를 추가한 부분 읽기...
        For Each xn As System.Xml.XmlNode In xdDoc.ChildNodes

            textBox1.Text += "RootNode = " + xn.Name + System.Environment.NewLine
            textBox1.Text += "하위 노드" + System.Environment.NewLine

            For Each xx As System.Xml.XmlNode In xn

                If xx.Name = "XMLFileEx" Then
                    textBox1.Text += "RootNode ->" + xx.Name + System.Environment.NewLine
                    textBox1.Text += "           " + xx.InnerText + System.Environment.NewLine
                ElseIf xx.Name = "ProgramCheckBox" Then
                    textBox1.Text += "RootNode ->" + xx.Name + System.Environment.NewLine
                    textBox1.Text += "           CheckBox1 = " + xx.Attributes(0).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           CheckBox2 = " + xx.Attributes(1).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           CheckBox3 = " + xx.Attributes(2).Value.ToString() + System.Environment.NewLine
                Else
                    textBox1.Text += "RootNode ->" + xx.Name + System.Environment.NewLine
                    textBox1.Text += "           Radio1 = " + xx.Attributes(0).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           Radio2 = " + xx.Attributes(1).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           Radio3 = " + xx.Attributes(2).Value.ToString() + System.Environment.NewLine
                End If

            Next

        Next

    End Sub

End Class

* 예제 결과

 

- 파일 저장 위치 및 파일 내용

 

아래의 그림은 XML 파일 만들기 버튼 클릭 이후 메모장으로 XML 파일 열어본 결과

파일 내용

 

결과 화면

 

https://kdsoft-zeros.tistory.com/64

 

[C#] XML File Write & Read 예제

* C# XML File Write & Read 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; u..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts