' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.Xml
Imports System.IO



Public Class treeview_form
    Public xmlfile As String


    Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)


        Dim xNode As XmlNode
        Dim tNode As TreeNode
        Dim nodeList As XmlNodeList
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            nodeList = inXmlNode.ChildNodes
            For i = 0 To nodeList.Count - 1
                xNode = inXmlNode.ChildNodes(i)
                inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
                tNode = inTreeNode.Nodes(i)
                AddNode(xNode, tNode)
            Next
        Else
            If Not inXmlNode.Attributes Is Nothing Then
                inTreeNode.Text = inXmlNode.Name
                For a = 0 To inXmlNode.Attributes.Count - 1
                    inTreeNode.Text = inTreeNode.Text & "  " & inXmlNode.Attributes.Item(a).Name & ":" & inXmlNode.Attributes.Item(a).Value
                Next
            Else
                'inTreeNode.Text = inXmlNode.Name.ToString & ": " & inXmlNode.InnerText.ToString
                inTreeNode.Text = inXmlNode.InnerText.ToString

            End If
        End If
       
    End Sub

    'Private Sub treeview_form_FormClosed(sender As Object, e As EventArgs) Handles Me.FormClosed
    '    If My.Computer.FileSystem.FileExists(xmlfile) Then
    '        Try
    '            If Me.Text = "ERREUR PHARMA-ML" Then

    '                My.Computer.FileSystem.MoveFile(xmlfile, Application.StartupPath & "\Pharma_ml\erreurs\" & Mid(xmlfile, InStrRev(xmlfile, "\") + 1), True)
    '            Else
    '                My.Computer.FileSystem.DeleteFile(xmlfile)
    '            End If
    '        Catch ex As Exception

    '        End Try

    '    End If
    'End Sub

    Private Sub treeview_form_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim xmldoc As New XmlDocument
        Dim xmlnode As XmlNode
        Dim fs As New FileStream(xmlfile, FileMode.Open, FileAccess.Read)
        xmldoc.Load(fs)
        xmlnode = xmldoc.ChildNodes(1)
        TreeView1.Nodes.Clear()
        TreeView1.Nodes.Add(New TreeNode(xmldoc.DocumentElement.Name))
        Dim tNode As TreeNode
        tNode = TreeView1.Nodes(0)
        AddNode(xmlnode, tNode)
        TreeView1.ExpandAll()
        fs.Close()
    End Sub
End Class