Imports System.Xml
Public Class ArinXML
    Private File As String
    Private vDocu As XmlDocument = Nothing
    Private nsmgr As XmlNamespaceManager
    Public Root As XmlElement
    Public ReadOnly Property Docu() As XmlDocument
        Get
            Return Me.vDocu
        End Get
    End Property
    Public ReadOnly Property Filename As String
        Get
            Return Me.File
        End Get
    End Property
    Public Sub New(ByVal Filename As String)
        Me.File = Filename
        If Exist() Then '//파일이 존재하면 도큐먼트반환
            Me.vDocu = New XmlDocument
            nsmgr = New XmlNamespaceManager(New Xml.NameTable)
            nsmgr.AddNamespace("x", "http://tindevil.com")
            Try
                Me.vDocu.Load(Filename)
                Root = vDocu.DocumentElement
            Catch ex As Exception
                Me.vDocu = Nothing
                Me.Root = Nothing
            End Try
        End If
    End Sub
    Public Sub New(ByVal docu As Xml.XmlDocument)
        Me.vDocu = docu
        Me.File = My.Application.Info.DirectoryPath & "\temp.xml"
        nsmgr = New XmlNamespaceManager(New Xml.NameTable)
        nsmgr.AddNamespace("x", "http://tindevil.com")
        Root = vDocu.DocumentElement
    End Sub
    ''' 
    ''' 파일존재여부
    ''' 
    ''' 
    ''' 
    Public Function Exist() As Boolean
        If Not vDocu Is Nothing Then Return True
        Return System.IO.File.Exists(Me.File)
    End Function
    Public Sub CreateFile()
        '//주어진파일명으로 기본파일을 생성한다.
        Dim NewXml As New System.Text.StringBuilder
        NewXml.AppendLine("")
        NewXml.AppendLine(" ")
        NewXml.AppendLine("")
        If System.IO.File.Exists(Me.File) Then System.IO.File.Delete(Me.File)
        My.Computer.FileSystem.WriteAllText(Me.File, NewXml.ToString.Replace("'", Chr(&H22)), False)
        Me.vDocu = New XmlDocument
        Me.vDocu.Load(Me.File)
        nsmgr = New XmlNamespaceManager(New Xml.NameTable)
        nsmgr.AddNamespace("x", "http://tindevil.com")
        Root = vDocu.DocumentElement
    End Sub
    Public Function Read(ByVal appkey As String, ByVal subkey As String, Optional ByVal defaltvalue As String = "", Optional ByVal Nullvalue As String = "") As String '//변수초기화
        '//파일이없을경우 빈값을 반환합니다.
        If Exist() = False Then Return ""
        Dim L As XmlElement = Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
        If L Is Nothing Then Return ""
        Dim C As XmlElement = L.SelectSingleNode(subkey, nsmgr)
        If C Is Nothing Then Return ""
        Return C.InnerText
    End Function
    Public Function Get_NameSpace() As XmlNamespaceManager
        Return Me.nsmgr
    End Function
    Public Function NS() As XmlNamespaceManager
        Return Me.nsmgr
    End Function
    Public Function CreateElement(ByVal name As String) As XmlElement
        Return Me.Docu.CreateElement(name, Me.NS.DefaultNamespace)
    End Function
    Public Function GetNode(ByVal appkey As String) As XmlElement
        '//파일이없을경우 빈값을 반환합니다.
        If Docu Is Nothing Then Return Nothing
        Return Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
    End Function
    Public Function GetNodes(ByVal appkey As String) As Xml.XmlNodeList
        '//파일이없을경우 빈값을 반환합니다.
        If Docu Is Nothing Then Return Nothing
        Dim L As XmlNodeList = Me.Root.SelectNodes(appkey, nsmgr) '//appkey를 먼저 조회한다.
        Return L
    End Function
    Public Property Data(ByVal appkey As String, ByVal subkey As String, Optional ByVal defaltvalue As Object = "", Optional ByVal trim As Boolean = False) As String
        Get
            '//파일이없을경우 빈값을 반환합니다.
            If Exist() = False Then Return defaltvalue
            Dim L As XmlElement = Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
            If L Is Nothing Then Return defaltvalue
            Dim C As XmlElement = L.SelectSingleNode(subkey, nsmgr)
            If C Is Nothing Then Return defaltvalue
            If trim Then
                Return C.InnerText.Trim
            Else
                Return C.InnerText
            End If
        End Get
        Set(ByVal value As String)
            '//파일이없을경우 빈값을 반환합니다.
            If Exist() = False Then Return
            Dim L As XmlElement = Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
            If L Is Nothing Then '//만들어야한다.
                L = Me.Docu.CreateElement(appkey)
                Me.Root.AppendChild(L)
            End If
            Dim C As XmlElement = L.SelectSingleNode(subkey, nsmgr)
            If C Is Nothing Then '//만들어야한다.
                C = Me.Docu.CreateElement(subkey)
                C.InnerText = value
                L.AppendChild(C)
            Else
                C.InnerText = value
            End If
            Me.Docu.Save(Me.File)
            'MsgBox(value)
        End Set
    End Property
    Public Sub Save()
        Me.Docu.Save(Me.File)
    End Sub
End Class