VS 2005 Search xml display results in datagrid-VBForums
Results 1 to 3 of 3

Thread: Search xml display results in datagrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    Question Search xml display results in datagrid

    Hey guys,
    I have a program I wrote a few years back and I brought it back to life with a search feature. I am using an XML file to store the data in. I have a search function that will display the first result from the textbox1.text value, it displays it in a messagebox. What I want to do is display all the results in a datagrid. Here is the code I have now.

    Code:
    Imports System.Xml
    Public Class search
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim xmlFile As XmlReader
            xmlFile = XmlReader.Create(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\train.xml", New XmlReaderSettings())
            Dim ds As New DataSet
            Dim dv As New DataView
            ds.ReadXml(xmlFile)
    
            dv = New DataView(ds.Tables(0))
            dv.Sort = "Name"
            Dim index As Integer = dv.Find(TextBox1.Text)
            If index = -1 Then
                MsgBox("Item Not Found")
            Else
    
                MsgBox(dv(index)("Name").ToString() & " " & dv(index)("type").ToString() & " " & dv(index)("Qty").ToString() & " " & dv(index)("Scale").ToString())
    
            End If
            xmlFile.Close()
            
    
        End Sub
    
        Private Sub search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    Is there a way to modify this code to show multiple results in a datagrid? For example, you have a locomotive and rolling stock named Santa Fe. I want to return all results for Santa Fe.

    Tfairris

  2. #2
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    448

    Re: Search xml display results in datagrid

    I like to use XmlNodeList to load the xml and iterate over the nodes. Here's how:

    Code:
    Private Sub parseXML()
           
                Dim doc As New XmlDocument()
                Dim i As Integer = 0
                Try
                    doc.Load(XmlFile) 'where XmlFile is the path to the file
                    Dim elemList As XmlNodeList = doc.GetElementsByTagName(START_NODE) 'where START_NODE is the top level node in your xml file.
    
                    For i = 0 To elemList.Count - 1                    
                        debug.print( elemList(i).ChildNodes(0).InnerXml)   'first level node
                       debug.print( elemList(i).ChildNodes(1).InnerXml)    'second node         
                    Next i
                 
                Catch ex As Exception
                    Throw
                End Try
            End Sub
    See if you can use something like this. this way, you can iterate over the xml. Once you are able to do that, you can use if...then statements to filter what you want, and add records to either an array or directly to the control. if you have any specific questions, let me know
    if i was able to help, rate my post!

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    Re: Search xml display results in datagrid

    Quote Originally Posted by jasonwucinski View Post
    I like to use XmlNodeList to load the xml and iterate over the nodes. Here's how:

    Code:
    Private Sub parseXML()
           
                Dim doc As New XmlDocument()
                Dim i As Integer = 0
                Try
                    doc.Load(XmlFile) 'where XmlFile is the path to the file
                    Dim elemList As XmlNodeList = doc.GetElementsByTagName(START_NODE) 'where START_NODE is the top level node in your xml file.
    
                    For i = 0 To elemList.Count - 1                    
                        debug.print( elemList(i).ChildNodes(0).InnerXml)   'first level node
                       debug.print( elemList(i).ChildNodes(1).InnerXml)    'second node         
                    Next i
                 
                Catch ex As Exception
                    Throw
                End Try
            End Sub
    See if you can use something like this. this way, you can iterate over the xml. Once you are able to do that, you can use if...then statements to filter what you want, and add records to either an array or directly to the control. if you have any specific questions, let me know
    Thanks jasonwucinski, I will give that a try and see what I can figure out.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.