none
Please steer me to the next step in getting data that is in a XML file, into a form so I can use data in a combo box and queries. RRS feed

  • Question

  • I am using VB.Net in Visual Studio 2017

    Please steer me to the next step in getting data that is in a XML file, into a form so I can use data in a combo box and queries.

    I think I would need to use “ReadXML” but found several versions like “ReadXml(Stream) and ReadXml(String) and ReadXml(TextReader) and ReadXml(XmlReader) or Load(IDataReader). 

    I was not able to tell if I need to somehow load data into a “DataTable” or a “DataCube” or “DataSet” or something else BEFORE I COULD USE THE DATA in the XML table in my VB.Net code.

    I feel like the kid told look it up in the dictionary. When have no idea how word is spelled. in this case, I do not know what method I need to learn about next.

    thank you for any help letting me know what I need to learn about to get and use data in VB.Net code.


    Mark J

    Thursday, August 10, 2017 7:05 AM

All replies

  • Hello,

    The following shows the basics for loading data from a xml file into a ComboBox and TextBox controls.

    The full source is downloaded here. All code was done back in 2011 so my guess is I did this in VS2008 thus newer versions of Visual Studio will prompt to upgrade the project. Bottom line is the source code is valid from VS2008 to VS2017.

    Public Class Form2
        Private DataSet1 As New DataSet
        WithEvents bsCustomers As New BindingSource
        Private FileName As String = "Customers.xml"
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            If IO.File.Exists(FileName) Then
                DataSet1.ReadXml(FileName)
                bsCustomers.DataSource = DataSet1.Tables("Customer")
    
                Dim dt = DataSet1.Tables(0)
    
                cboCompany.DisplayMember = "CompanyName"
                cboCompany.DataSource = bsCustomers
    
                Label1.DataBindings.Add("Text", bsCustomers, "ContactName")
                Label2.DataBindings.Add("Text", bsCustomers, "Address")
                Label3.DataBindings.Add("Text", bsCustomers, "City")
    
            Else
                MsgBox("Failed to locate " & FileName)
            End If
    
        End Sub
    End Class

    There is also the following code that is not setup as the main form but by changing the main form this will give you more to learn from.

    ''' <summary>
    ''' 1. Read XML data into a DataSet w/o specifying schema for the XML data.
    ''' 2. Shows column names from the DataTable read in from step 1 into a ListBox
    ''' 3. Show all DataRows from the DataTable read in from step 1.
    ''' 4. LINQ statement against XML data in the DataTable to return select fields
    '''    for customers from Germany.
    ''' 5. Display German customer to VS2008 output window
    ''' 6. Loop thru our DataTable using a For-Each statement looking for German customers
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Form1
        Private Const HeaderForGermanCustomers As String = "Company name                      City"
    
        Private DataSet1 As New DataSet
        Private FileName As String = "Customers.xml"
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'ListBox1.DataSource = (From N In Enumerable.Range(0, 101).ToList Where N.IsEven).ToList
            German()
        End Sub
    
        Private Sub German()
            If IO.File.Exists(FileName) Then
                '
                ' Here we read in the XML file specified under the private variable FileName (see above)
                '
                DataSet1.ReadXml(FileName)
                '
                ' Referernce the only DataTable in DataSet1
                ' You could also use DataSet1.Tables(0) rather than dt for usage below.
                '
                Dim dt = DataSet1.Tables(0)
    
                ' Show Columns
                ListBox1.DataSource = (From Column In dt.Columns.Cast(Of DataColumn)() _
                                       Select Column.ColumnName).ToList
    
                ' uncomment the following for/next to view contents of first two column data for all rows
                'For Each Row As DataRow In dt.Rows
                '    Console.WriteLine("[{0}] [{1}]", Row(0), Row(1))
                'Next
    
                DataGridView1.AllowUserToAddRows = False
                DataGridView1.DataSource = dt
    
                ' select rows where Country field/column is Germany
                Dim GermanyCustomers = (From Customer In dt.Rows.Cast(Of DataRow)() _
                                        Where Customer.Field(Of String)("Country") = "Germany" _
                                        Select Identifier = Customer.Field(Of String)("CustomerID"), _
                                        Name = Customer.Field(Of String)("CompanyName"), _
                                        City = Customer.Field(Of String)("City") _
                                        Order By City Descending).ToList
    
                Console.WriteLine("German customers (ordered by city in decending order via LINQ")
                Console.WriteLine(HeaderForGermanCustomers)
                For Each customer In GermanyCustomers
                    Console.WriteLine("{0,-33} {1}", customer.Name, customer.City)
                Next
    
                Console.WriteLine()
    
                ' the long way to get german customers
                Console.WriteLine("German customers using a for/each")
                Console.WriteLine(HeaderForGermanCustomers)
                For Each row As DataRow In dt.Rows
                    If row.Field(Of String)("Country") = "Germany" Then
                        Console.WriteLine("{0,-33} {1}", row.Field(Of String)("CompanyName"), row.Field(Of String)("City"))
                    End If
    
                Next
    
    
            Else
                MsgBox("Failed to locate " & FileName)
            End If
    
            Console.WriteLine()
    
    
    
        End Sub
        Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) 'Handles DataGridView1.CellMouseEnter
            If e.ColumnIndex >= 0 Then
                Dim CellPoint As Point = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
                Dim GridPoint As Point = DataGridView1.Location
                Dim PointLocation As Point
                PointLocation.X = GridPoint.X + CellPoint.X
                PointLocation.Y = GridPoint.Y + CellPoint.Y
                Me.Button1.Location = PointLocation
            End If
        End Sub
    
    
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cor Ligthert Friday, August 11, 2017 7:24 AM
    Thursday, August 10, 2017 9:19 AM
    Moderator
  • Karen Payne,

    thanks very much, for the quick answer, and comments on what some of the lines of code does. 

    Those comments also answered some of my questions that I did not know what terms to use when asking the questions I had.

    Now I will be studying your code, to understand, and then modify the code to work with the first table.

    thanks again.

    Mark J


    Mark J

    Thursday, August 10, 2017 10:27 AM
  • On another forum a user asked about adding some parts of an XML file, the Name attribute of the Label tag, to a combobox.  Here is how:

            Dim xe As XElement
            ' to load from a file
            ' Dim yourpath As String = "your path here"
            'xe = XElement.Load(yourpath)
    
            ' for testing
            xe = <VersionedFile EntityPath="/Version2017/Build/labels.txt"
                     Name="customerlabels.txt"
                     ArchivePath="\\Pvcs2000\PVCS\PVCS\archives\Version85\Build\labels.txt-arc"
                     ArchiveOwner="smith"
                     CreateTime="1462823446000"
                     LockCount="0"
                     RevisionCount="1"
                     WorkPath="C:\BuildPath"
                     WorkPathNamespace="0">
                     <WorkfileDescription>initial put of file
    </WorkfileDescription>
                     <Revision EntityPath="/Version85/Build/labels.txt/1.0"
                         Name="1.0"
                         Author="SMITH"
                         LockCount="0"
                         ModificationDate="1134051344000"
                         CheckedInDate="1462823446000">
                         <ChangeDescription>Initial revision.
    </ChangeDescription>
                         <Label Name="CLIENT_ABC_9999"/>
                         <Label Name="CLIENT_DEF_8888"/>
                         <Label Name="CLIENT_GHI_7777"/>
                         <Label Name="CLIENT_JKL_6666"/>
                         <Label Name="CLIENT_MNO_5555"/>
                         <Label Name="CLIENT_PQR_4444"/>
                         <Label Name="CLIENT_STU_3333"/>
                         <Label Name="CLIENT_VXZ_2222"/>
                     </Revision>
                 </VersionedFile>
    
            Dim lbls As List(Of String) = (From el In xe.<Revision>...<Label> Select el.@Name).ToList
            ComboBox1.DataSource = lbls
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, August 10, 2017 10:34 AM
  • Hi PuzzledByWord,

    According to your description, you want to read xml file into datatable, then you can use combobox to query data, please refer to the code below.

    Dim myXMLfile As String = "D:\test\TestXML.xml"
            Dim ds As New DataSet()
            ' Create new FileStream with which to read the schema.
            Dim fsReadXml As New System.IO.FileStream(myXMLfile, System.IO.FileMode.Open)
            Try
                ds.ReadXml(fsReadXml)
                DataGridView1.DataSource = ds
                DataGridView1.DataMember = "fullname"
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            Finally
                fsReadXml.Close()
            End Try

    Here is my xml file:

    And you can refer to this article to understand the ReadXml(Stream) and ReadXml(String) and ReadXml(TextReader) and ReadXml(XmlReader) .

    https://support.microsoft.com/en-sg/help/309702/how-to-read-xml-data-into-a-dataset-by-using-visual-basic--net

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 11, 2017 2:47 AM
    Moderator