I really tried with the title! I could not find what I was looking for through google or here. Seemed like something that could be common enough that I'd find it online already but perhaps it's so simple that no one had a problem doing it so they did not have to post about it online haha

I have an XML file where each node except for one has the same number of children and all the same format. I've had no problem creating a DataGridView when only these similar nodes are available. But a wrench gets thrown in with the addition of another node. This discussion does not regard the specific XML file which is shown as I created it for the purpose of clarification. I may run into this fairly often if use this structure so I felt that it was best to figure this out now. The file shown below would be named TransactionID.xml so for transaction # 00001 this would be 00001.xml which is only important in the DGV section.

    <?xml version="1.0" encoding="utf-8"?>
    <!--Transaction File.-->
    <Transaction>
      <TransactionTotal>000000001</TransactionTotal>
       <Items>
        <Item>
         <ItemID>1234</ItemID>
         <PriceEach>100.00</PriceEach>
         <Purchased>2</Purchased>
         <ItemTotal>200.00</ItemTotal>
       </Item>
       <Item>
        <ItemID>2345</ItemID>
         <PriceEach>2.50</PriceEach>
         <Purchased>3</Purchased>
         <ItemTotal>7.50</ItemTotal>
       </Item>
       <Item>
         <ItemID>3456</ItemID>
         <PriceEach>1.00</PriceEach>
         <Purchased>12</Purchased>
         <ItemTotal>12.00</ItemTotal>
       </Item>
      </Items>
     </Transaction> 

As one can see, the XML file concerns a single transaction with a node for each specific item that was purchased. However, there is another node on the same level as each item which is the transaction's ID. Without that node I can EASILY create my DGV like thus (TransactionID would be sent to the following code by the user selecting a particular transaction from a list of transactions.):

    Dim TID = TransactionID & ".xml"
    Dim Transaction As New StreamReader(TID)
    Dim TransactionItems As New DataSet
    TransactionItems.ReadXml(Transaction)
    DataGridView1.DataSource = TransactionItems.Tables(0)
    Transaction.Close()

This does the job perfectly fine if the XML file contains only the Item nodes. But it doesn't work with the TransactionTotal node thrown in. The DGV just remains blank. I do use 'Try' and 'Catch' to check for the existence AND the validity of the XML file and those do not return any errors. The XML does validate but it is being thrown off by the two different node 'types'.

In my XML file I've tried to separate the TransactionTotal from the Items as best as I could in my Schema and ideally I would have a label to show the TransactionTotal outside of the DVG.

        Dim TID = TransactionID & ".xml"
        Dim Transaction As New StreamReader(TID)
        Dim TransactionItems As New DataSet
        TransactionItems.ReadXml(Transaction)
        DataGridView1.DataSource = TransactionItems.Tables(0)
        Transaction.Close()
        TransactionTotalLabel.Text = TransactionTotal

I apologize for the verbosity of this post but I am trying to be as clear as is possible. I would gladly answer any questions or provide more info in the name of me figuring this out. I have read a plethora of articles and tutorials on XML-to-DataGridView and I've yet to figure out how to best handle this despite this schema being really quite a simple one. Much appreciation to anyone and everyone that cares to chime in with their two cents!

I think what's happening is that the dataset is creating a separate table for each node. You're using table 0 as the datasource, try using table 2.

To display the Transaction ID with the table, you can dock the datagridview inside a groupbox with its text property to the Transaction ID which will be:

   TransactionItems.Tables(0).Rows(0).Item(0).ToString()

Edited 1 Year Ago by tinstaafl

You are a genius! Thank you so much! With all of the time I spent trying to figure this problem out myself I had negelected to pay enough attention to the "Tables" aspect. Honestly the code to put the original XML into a DGV came from a site and obviously I did not really catch WHY/HOW it placed the transaction info into the DGV. So not only have you provided me with the answer I so desparately needed but you also helped me better understand what I was doing in the first place. A million thanks!

This question has already been answered. Start a new discussion instead.