2

I've spent a great deal of time researching a solution to my issue with very little success. I'm rather new with .NET and vb.net so I apologize if this seems simple to anyone else. I had a first version of what I am trying to do which worked fine. I will provide sample XML and the vb.net code I used to render it. As I decided that I needed to add an additional node to the XML file I am no longer sure how to process it.

Original XML:

<?xml version="1.0" encoding="utf-8"?>
<!--Transaction File.-->
<Transaction>
  <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>
  </Items>
 </Transaction>

Vb.net code to populate DataGridView with each item being its own row.

 Dim Transaction As New DataSet
     Transaction.ReadXml(TransactionID) 'TransactionID is sent to this code
     DataGridView1.DataSource = Transaction.Tables(0)

This works perfectly. However, I decided that I needed to add a new node to hold the total cost for the specific transaction. So the new XML file is as such:

<?xml version="1.0" encoding="utf-8"?>
<!--Transaction File.-->
<Transaction>
  <TransactionTotal>207.50</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>
  </Items>
 </Transaction>

So now I have a new tagset of TransactionTotal which should be displayed as the text property of a label titled TransactionTotal. TransactionTotal.Text = ?? The TransactionTotal tags are on the same level as the Items tags. And then The DataGridView should show all of each Item within Items as a new row in the DGV.

Obviously this new node makes my original DGV populate code invalid. I use a Try/Catch statement which throws no errors because the XML file is found and it validates as proper XML but my DGV is blank as the code is thrown off by TransactionTotal being added into the mix.

I greatly appreciate any help leading me the right way!

1

My apologies but a muse came along and I have an answer to my own question which I wanted to post here in case it helps someone else. However, if you have any other ideas of how to do this then please share.

Updated Code:

    Dim TransactionItems As New DataSet
    TransactionItems.ReadXml(TransactionID) ' still passed into this code by user selection
    DataGridView1.DataSource = TransactionItems.Tables(2)
    TransactionTotal.Text = TransactionItems.Tables(0).Rows(0).Item(0).ToString()

Honestly in my original code I did not really understand the "Tables" part of the code to put the Items into the DataGridView. As it turns out each node was being turned into a separate table. Now the value between the TransactionTotal tags is set as the text property of a label.

  • Note that if you need to cater for multiple transactions in a file, if you just slip <TransactionTotal> inside the <Items>, you can describe multiple transactions with just multiple <Items> groups, and they are read into two tables with an Items_Id to join them. – Mark Hurd Aug 21 '15 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.