Results 1 to 2 of 2

Thread: How to navigate records from XML File?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Question How to navigate records from XML File?

    This code creates XML File
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim connetionString As String
            Dim connection As SqlConnection
            Dim adapter As SqlDataAdapter
            Dim ds As New DataSet
            Dim sql As String
    
            connetionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Danial\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Pay.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
            connection = New SqlConnection(connetionString)
            sql = "select * from Acc"
            Try
                If IO.File.Exists("Testimage.xml") = False Then
                    connection.Open()
                    adapter = New SqlDataAdapter(sql, connection)
                    adapter.Fill(ds)
                    connection.Close()
                    ds.WriteXml("Testimage.xml")
                    MsgBox("File Created")
                Else
                    MsgBox("File Already Exists")
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub

    This code reads the file and send it's contents to text box(es)

    Code:
     Dim xmlFile As XmlReader
            xmlFile = XmlReader.Create("Testimage.xml", New XmlReaderSettings())
            Dim ds As New DataSet
            While ds.ReadXml(xmlFile)
                Dim i As Integer
                For i = 0 To ds.Tables(0).Rows.Count - 1
                    TextBox1.Text = (ds.Tables(0).Rows(i).Item(1))
                    TextBox2.Text = (ds.Tables(0).Rows(i).Item(2))
                    TextBox3.Text = (ds.Tables(0).Rows(i).Item(3))
                    TextBox4.Text = (ds.Tables(0).Rows(i).Item(4))
                    TextBox5.Text = (ds.Tables(0).Rows(i).Item(5))
                    TextBox6.Text = (ds.Tables(0).Rows(i).Item(6))
                    TextBox7.Text = (ds.Tables(0).Rows(i).Item(7))
                    TextBox8.Text = (ds.Tables(0).Rows(i).Item(8))
                    TextBox9.Text = (ds.Tables(0).Rows(i).Item(9))
                Next
            End While
    End Sub

    This code Navigates to the next record....and needs help
    Code:
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                Dim nav As XPathNavigator
                Dim docNav As XPathDocument
                docNav = New XPathDocument("Testimage.xml")
                nav = docNav.CreateNavigator
                nav.MoveToRoot()
                nav.MoveToNext()
                Do
                    Dim i As Integer
                    i = +1
                Loop While nav.MoveToNext
                'Pause.
                Console.ReadLine()
        		End Sub
    Last edited by chdboy; Feb 26th, 2013 at 11:18 AM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to navigate records from XML File?

    But it doesn't navigate to the next, it navigates to the first ...

    nav = docNav.CreateNavigator
    nav.MoveToRoot()
    nav.MoveToNext()

    .. and then it doesn't move again (no idea what Loop While nav.MoveToNext is suposed to do!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width