Imports System.Xml
Imports System.Xml.XPath
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnShowTop10 As System.Windows.Forms.Button
    Friend WithEvents btnChangeAndSave As System.Windows.Forms.Button
    Friend WithEvents lstResults As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnShowTop10 = New System.Windows.Forms.Button
        Me.btnChangeAndSave = New System.Windows.Forms.Button
        Me.lstResults = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'btnShowTop10
        '
        Me.btnShowTop10.Location = New System.Drawing.Point(32, 264)
        Me.btnShowTop10.Name = "btnShowTop10"
        Me.btnShowTop10.Size = New System.Drawing.Size(120, 40)
        Me.btnShowTop10.TabIndex = 1
        Me.btnShowTop10.Text = "Wyświetl pierwsze 10 imion"
        '
        'btnChangeAndSave
        '
        Me.btnChangeAndSave.Location = New System.Drawing.Point(216, 264)
        Me.btnChangeAndSave.Name = "btnChangeAndSave"
        Me.btnChangeAndSave.Size = New System.Drawing.Size(120, 40)
        Me.btnChangeAndSave.TabIndex = 2
        Me.btnChangeAndSave.Text = "Zmień i zapisz"
        '
        'lstResults
        '
        Me.lstResults.Location = New System.Drawing.Point(8, 8)
        Me.lstResults.Name = "lstResults"
        Me.lstResults.Size = New System.Drawing.Size(344, 238)
        Me.lstResults.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 317)
        Me.Controls.Add(Me.lstResults)
        Me.Controls.Add(Me.btnChangeAndSave)
        Me.Controls.Add(Me.btnShowTop10)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

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

    Private Sub ShowTop10()
        Dim xDoc As New XmlDocument
        xDoc.Load("simple2.xml")
        'Do uzyskiwania dostępu do atrybutu elementu użyto składni języka XPATH
        Dim xNodeList As XmlNodeList = xDoc.SelectNodes("descendant::tblKlienci/@Imie")
        Dim xNode As XmlNode
        Dim i As Integer = 0
        For Each xNode In xNodeList
            lstResults.Items.Insert(i, xNode.InnerText)
            i = i + 1
        Next
    End Sub

    Public Sub ChangeNameandSave(ByVal NameToChange As String, ByVal ChangeTo As String)
        Dim xDoc As New XmlDocument()
        xDoc.Load("simple2.xml")
        Dim xNodeList As XmlNodeList = xDoc.SelectNodes("descendant::tblKlienci/@Imie")
        Dim xNode As XmlNode
        For Each xNode In xNodeList
            If xNode.InnerText = NameToChange Then
                xNode.Value = ChangeTo
            End If
        Next
        xDoc.Save("simple2.xml")
        MsgBox("Zmiana imienia została zapisana!", 0)
        lstResults.Items.Clear()
        ShowTop10()
    End Sub

    Private Sub btnShowTop10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTop10.Click
        ShowTop10()
    End Sub

    Private Sub lstResults_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstResults.DoubleClick
        Dim oldName As String = lstResults.GetItemText(lstResults.Items.Item(lstResults.SelectedIndex))
        Dim newName As String = InputBox("Proszę wpisać nowe imię", "Zmień i zapisz")
        ChangeNameandSave(oldName, newName)
    End Sub
End Class
