[VB 2008] XML 문서예제 본문

[PL]/VB & VB.NET

[VB 2008] XML 문서예제

객과 함께. 2010. 1. 19. 00:55

인터넷 서핑중에 블러그 주인장님 들께서 쉽게 설명 한 블러그에서 소스를 받아서  제가 이해할 정도로 해서 수정 하였습니다.

보시고 블러그 주인장님께서 답변란에 남겨 주시면 내리도록 하겠습니다. .

 

 

Imports System.Xml
Imports System.IO
Imports System.Text

Public Class Form1
    '''
    '''  프로그램명 :  XML 생성 및 XML 읽기
    
    Public Structure CreateXmlDeclaration
        Public version
        Public encoding
        Public standalone
    End Structure

    Public Structure Pointer
        Public ID As Int32
        Public Name As String
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Text = "XML File 생성 및 일기 예제"
        TextBox1.Text = ""
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer

        TextBox1.Text = ""

        Dim doc = New XmlDocument

        doc.AppendChild(doc.CreateXmlDeclaration("1.0", "euc-kr", "yes"))

        Dim Strdata() As String = {"서울대학교", "연세대학교", "고려대학교"}

        Dim root As XmlNode = doc.CreateElement(String.Empty, "Root", String.Empty)

        For i = 0 To Strdata.Length - 1
            Dim child As XmlNode = doc.CreateElement(String.Empty, "Record", String.Empty)
            Dim childID As XmlNode = doc.CreateElement(String.Empty, "ID", String.Empty)
            Dim childNAME As XmlNode = doc.CreateElement(String.Empty, "Name", String.Empty)

            childID.InnerXml = (i + 1).ToString()
            childNAME.InnerXml = Strdata(i)
            child.AppendChild(childID)
            child.AppendChild(childNAME)
            root.AppendChild(child)
        Next

        doc.AppendChild(root)
        doc.Save("XmlText.xml")

        TextBox1.Text = "XML 문서가 생성 되었습니다."
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        TextBox1.Text = ""

        Dim i As Int32 = 1
        Dim tempstruct As Pointer

        If File.Exists(Application.StartupPath & "\XmlText.xml") Then
            ' UTF-8로 인코딩된 것을 읽기
            Dim rs As New StreamReader(Application.StartupPath & "\XmlText.xml", Encoding.Default)
            Dim rx As New XmlTextReader(rs)

            Do While rx.Read
                If rx.NodeType = Xml.XmlNodeType.Element Then
                    If rx.Name = "Record" Then '요소가 item이면
                        tempstruct = ReadSubTree(rx.ReadSubtree)
                        Printtxt(tempstruct.ID & ".  " & tempstruct.Name) ' name 값
                        i += 1
                    End If
                End If
            Loop

            Dim doc As New Xml.XmlDocument
            doc.Load(Application.StartupPath & "\XmlText.xml")
            Dim ElementEx As Xml.XmlElement = doc.SelectSingleNode("/Root")
            doc = Nothing
            rx.Close()
            rs.Close()
        Else

            TextBox1.Text = "해당하는 XML문서가 없습니다."
            Exit Sub
        End If
    End Sub

    Private Sub Printtxt(ByVal str As String)

        TextBox1.Text = TextBox1.Text & str & vbCrLf
    End Sub

    Private Function ReadSubTree(ByRef rx As Xml.XmlReader) As Pointer

        ' 하위 구조 읽기
        Dim RetStruct As Pointer
        Do While rx.Read
            If rx.Name = "ID" Then
                RetStruct.ID = CInt(Val(rx.ReadString))
            ElseIf rx.Name = "Name" Then
                RetStruct.Name = rx.ReadString
            End If
        Loop

        Return RetStruct
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Me.Close()
        End
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim Yn As Boolean
        Dim FileNum As Integer
        Dim FileName As String

        If TextBox1.Text <> "" Then
            Yn = MsgBox("저장 하시겠습니까?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes

            If Yn = True Then

                FileNum = FreeFile()
                FileName = Directory.GetCurrentDirectory & "\Test.txt"
                FileOpen(FileNum, FileName, OpenMode.Output)
                Print(FileNum, TextBox1.Text)
                FileClose(FileNum)
                TextBox1.Text = ""

            Else
                TextBox1.Text = ""
            End If
        Else
            TextBox1.Text = ""
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Dim FileNum As Integer
        Dim FileName As String
        Dim txtLine As String

        If Not File.Exists("Test.txt") Then

            TextBox1.Text = "해당 파일이 없습니다."
            Exit Sub
        Else

            FileNum = FreeFile()
            FileName = Directory.GetCurrentDirectory & "\Test.txt"
            FileOpen(FileNum, FileName, OpenMode.Input)

            Do While Not EOF(FileNum)
                Input(FileNum, txtLine)
                TextBox1.Text = TextBox1.Text & txtLine & vbCrLf
            Loop

            FileClose(FileNum)
        End If
    End Sub
End Class

'[PL] > VB & VB.NET' 카테고리의 다른 글

명령줄 컴파일 하기.  (0) 2010.08.13
ManagementPath.RelativePath 속성  (0) 2010.06.26
[ VB 2008 ] 데이터베이스예제 .sdf  (0) 2010.01.19
[ VB 2008 ] COLOR View예제.  (0) 2010.01.11
[ VB 2008 ] TreeView 예제  (0) 2010.01.10