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!