Results 1 to 16 of 16

Thread: Importing an xml file into a datagridview not all data displayed

  1. #1

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Importing an xml file into a datagridview not all data displayed

    Hi,
    It's been a couple of years since I've done anything with VB but I have an xml file that I would like to view and navigate in a windows form application.

    The xml file is a fixed file I have no control over.

    I've limited experience with xml files but using google I was able to setup a form with a couple of datagridviews and load different section of the file into them using a dataset and ReadXml.

    The issue I'm having is that the DGV only shows the first level of information and not any of the child sections.

    So my question is can anyone help out or point towards some tutorials that might help me to get to view the child information.

    Here is an example of the xml file structure, the data highlighted in Blue is displayed in the grid but the data under <LEVEL10> is not displayed at all, how can I view Levels below level 10
    Name:  xml sample.jpg
Views: 1031
Size:  43.1 KB

    Thanks,
    Phil
    Last edited by gadjet; Feb 28th, 2018 at 04:23 AM. Reason: tupos

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Importing an xml file into a datagridview not all data displayed

    How many tables are in the table collection of the dataset after reading the xml?

  3. #3

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Sorry, I don't know, I use this code to load an xml file into the dataset and populate the DGV

    Code:
            AuthorsDataSet.ReadXml(filePath)
    
            DataGridView1.DataSource = AuthorsDataSet
            DataGridView1.DataMember = "LEVEL1"

  4. #4
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Importing an xml file into a datagridview not all data displayed

    Put a button and insert this code. What is the output?
    Code:
        Private Sub Button_GetTables_Click(sender As Object, e As EventArgs) Handles Button_GetTables.Click
            Dim TblInfo As String = String.Empty
            For Each Tbl As DataTable In Dset.Tables
                TblInfo &= Tbl.TableName & Environment.NewLine
            Next
            MsgBox(TblInfo)
        End Sub

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Importing an xml file into a datagridview not all data displayed

    That's because the DGV doesn't display hierarchical information. It's designed to display tabular data.
    If you want it displayed, you'll either need to change your display control to something that does display data in a hierarchical way... OR you'll have to manipulate the XML first before loading it to get it into the format you want prior to displaying it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Quote Originally Posted by kpmc View Post
    Put a button and insert this code. What is the output?
    Code:
        Private Sub Button_GetTables_Click(sender As Object, e As EventArgs) Handles Button_GetTables.Click
            Dim TblInfo As String = String.Empty
            For Each Tbl As DataTable In Dset.Tables
                TblInfo &= Tbl.TableName & Environment.NewLine
            Next
            MsgBox(TblInfo)
        End Sub
    Thanks, that gives me a list of tables that seem to match the different sections within the xml file, so I suppose that means I can work with tables to display the different sections and link the data between the tables so they are in sync.

  7. #7

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Quote Originally Posted by techgnome View Post
    That's because the DGV doesn't display hierarchical information. It's designed to display tabular data.
    If you want it displayed, you'll either need to change your display control to something that does display data in a hierarchical way... OR you'll have to manipulate the XML first before loading it to get it into the format you want prior to displaying it.

    -tg
    Yes, my plan is to have secondary DGVs and link the data but I need to identify the keys that I can use in the xml file and they are in the attributes (I think that's what they are called), I will need to figure out how to reference an attribute within the section for a filter to work.

  8. #8
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Importing an xml file into a datagridview not all data displayed

    You need to create relations. Here is an example out of my snippets

    Code:
            Dim dataRelation As DataRelation
            Dim dataColumn1 As DataColumn, dataColumn2 As DataColumn
            dataColumn1 = ds.Tables("Masters").Columns("CustomerID")
            dataColumn2 = ds.Tables("Detailed").Columns("_CustomerID")
            dataRelation = New DataRelation("CustomersToOrders", dataColumn1, dataColumn2)
    
    
            ds.Relations.Add(dataRelation)
            masterBindingSource.DataSource = ds
            masterBindingSource.DataMember = "Masters"
    
            detailsBindingSource.DataSource = masterBindingSource
            detailsBindingSource.DataMember = "CustomersToOrders"
    
    
            DataGridView1.DataSource = masterBindingSource
            DataGridView2.DataSource = detailsBindingSource
    You may also be interested in the "GetChildRows" method

  9. #9

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Quote Originally Posted by kpmc View Post
    You need to create relations. Here is an example out of my snippets

    Code:
            Dim dataRelation As DataRelation
            Dim dataColumn1 As DataColumn, dataColumn2 As DataColumn
            dataColumn1 = ds.Tables("Masters").Columns("CustomerID")
            dataColumn2 = ds.Tables("Detailed").Columns("_CustomerID")
            dataRelation = New DataRelation("CustomersToOrders", dataColumn1, dataColumn2)
    
    
            ds.Relations.Add(dataRelation)
            masterBindingSource.DataSource = ds
            masterBindingSource.DataMember = "Masters"
    
            detailsBindingSource.DataSource = masterBindingSource
            detailsBindingSource.DataMember = "CustomersToOrders"
    
    
            DataGridView1.DataSource = masterBindingSource
            DataGridView2.DataSource = detailsBindingSource
    You may also be interested in the "GetChildRows" method
    Thanks, I will try this and see if I can get it to work

  10. #10

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Having now created a listbox that populates with all the tables and linked that to a GDV I can now see each table and view the data, this has led me to confirm that there are no master/child keys in the tables ??

    The relationship between the data tables must be based on position within the file, should I maybe look at parsing the file and organise the data somehow.

    in the screen grab in the first post, there are separate tables for LEVEL1, LEVEL11, LEVEL14 and LEVEL1 is repeated but in reality LEVEL11 and LEVEL14 are nested within LEVEL1 without any master/child keys.

    Unfortunately I can't post the actual xml file due to its content being confidential.

    Thanks for the help so far.

  11. #11
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Importing an xml file into a datagridview not all data displayed

    I am far from an XML expert. I despise it actually. In some places it is convenient, but those are all cases where you dont have to look at its ugliness, where the datatable creates and reads it. Getting XML from outside source is comparable to doing a word search puzzle that doesnt actually have any words.

    Anyway, if what you're saying is there is no "ID" column you can use to relate the tables by, then I cant help you. If you have access to the system that creates the xml I would recommend creating such a column. GUIDs work well if you cant create an ID by any other means.

  12. #12
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Importing an xml file into a datagridview not all data displayed

    Getting XML from outside source is comparable to doing a word search puzzle that doesnt actually have any words.
    And the puzzle is on fire, and you are on fire, and you are in hell.

  13. #13

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Quote Originally Posted by kpmc View Post
    And the puzzle is on fire, and you are on fire, and you are in hell.
    I'm definitely getting that feeling.

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Importing an xml file into a datagridview not all data displayed

    I find that to be true in cases where the XML isn't designed well, which it sounds like is the case here, or where it is being used in a manner which it wasn't intended or incompatible, which it seems like the case here... which is like 80% of the time.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    59

    Re: Importing an xml file into a datagridview not all data displayed

    Quote Originally Posted by techgnome View Post
    I find that to be true in cases where the XML isn't designed well, which it sounds like is the case here, or where it is being used in a manner which it wasn't intended or incompatible, which it seems like the case here... which is like 80% of the time.

    -tg
    Yes, unfortunately the xml is generated automatically from an application from our parent company, they have some tools but they are a bit difficult to use and I wanted to just have a quick and easy way to view and navigate the xml data.

  16. #16
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Importing an xml file into a datagridview not all data displayed

    TG-
    ANDRÉ RIEU is coming to the US in Sep this year. Not entirely sure where you are geographically situated, but I just got notified of this announcement.

    https://www.andrerieu.com/en/tour

    -PS, sorry about the off topicness

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