I´m trying to populate a TreeView component in VB.NET from a XML file. It´s a large XML file and I chopped it up in order to give you guys an idea about how it is structured. I´m new to VB.NET so I´m having a hard time to figure this out. Below there´s an image of the order I expect this data to be shown in the TreeView, and I added some arrows so you can understand where the data is supposed to come from in the XML structure.
I found and tried to adapt a code I got from a Microsoft tutorial, but it´s based on a much simpler XML structure and it´s not working with my file.
The arrows in the image are being shown out of synch with the XML tags & nodes but my goal was to avoid the arrows and rectangles overlaping each other making the picture unreadable.
When we have a node named "Cutter" it´s a different type of component, and more than one may be comprised within the current "Cutter" or "Holder" node, so they are separated by the "Reference" tag. If it´s named "Holder" it´s another component type on the tool stackup... Every new tool starts with an element named "Tool".
Below the picture what I´m trying to do in the code:
Private Sub btnSearchTLS_Click(sender As Object, e As EventArgs) Handles btnSearchTLS.Click
Try
' SECTION 1. Create a DOM Document and load the XML data into it.
Dim dom As New XmlDocument()
dom.Load(txtFileOpenTLS_2.Text)
' SECTION 2. Initialize the treeview control.
tviewToolIDs.Nodes.Clear()
tviewToolIDs.Nodes.Add(New TreeNode(dom.DocumentElement.Name))
Dim tNode As New TreeNode()
tNode = tviewToolIDs.Nodes(0)
' SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode)
tviewToolIDs.ExpandAll()
Catch xmlEx As XmlException
MessageBox.Show(xmlEx.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
Dim xNode As XmlNode
Dim tNode As TreeNode
Dim nodeList As XmlNodeList
Dim i As Integer
' Loop through the XML nodes until the leaf is reached.
' Add the nodes to the TreeView during the looping process.
If inXmlNode.HasChildNodes() Then
nodeList = inXmlNode.ChildNodes
For i = 0 To nodeList.Count - 1
xNode = inXmlNode.(i)
inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
tNode = inTreeNode.Nodes(i)
AddNode(xNode, tNode)
Next
Else
' Here you need to pull the data from the XmlNode based on the
' type of node, whether attribute values are required, and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim
End If
End Sub
Here´s the XML file:
<?xml version="1.0"?>
<ToolLibrary Version="7.3">
<Tools>
<Tool ID="100004" Units="Millimeter">
<Description>100004</Description>
<Teeth>1</Teeth>
<Type>Turning</Type>
<Cutter>
<Reference ID="12140 - N123G2-0318-0008">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Torneamento - Insertos paramétricos</ToolID>
<EntityName>12140 - N123G2-0318-0008</EntityName>
<Origin>
<X>34.2</X>
<Y>0</Y>
<Z>-82.3</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>-90</Y>
<Z>0</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Cutter>
<Holder>
<Reference ID="12135 - C5-570-32-LF">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12135 - C5-570-32-LF</EntityName>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="12139 - 570-32L123G18B130A">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12139 - 570-32L123G18B130A</EntityName>
<Origin>
<X>21</X>
<Y>0</Y>
<Z>-42</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Holder>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>35</X>
<Y>0</Y>
<Z>-83.1</Z>
</DrivenPoint>
<DrivenPoint ID="2">
<Type>-1</Type>
<Radius>0</Radius>
<X>31.82</X>
<Y>0</Y>
<Z>-83.1</Z>
</DrivenPoint>
<CutterCompensation ID="1">0.8</CutterCompensation>
<CutterCompensation ID="2">0.8</CutterCompensation>
</Tool>
<Tool ID="100171" Units="Millimeter">
<Description>100171</Description>
<Teeth>1</Teeth>
<Type>Turning</Type>
<Cutter>
<Reference ID="12349 - CNMG120412">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Torneamento - Insertos paramétricos</ToolID>
<EntityName>12349 - CNMG120412</EntityName>
<Origin>
<X>-35.8</X>
<Y>0</Y>
<Z>-291.8</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>-5</Y>
<Z>0</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Cutter>
<Holder>
<Reference ID="11977 - C6-390.410-100-110A">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>11977 - C6-390.410-100-110A</EntityName>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="11978 - C6-570-2C-60-148-40L">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>11978 - C6-570-2C-60-148-40L</EntityName>
<Origin>
<X>0</X>
<Y>0</Y>
<Z>-110</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="11979 - SL-PCLNL-40-12HP">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>11979 - SL-PCLNL-40-12HP</EntityName>
<Origin>
<X>-10</X>
<Y>0</Y>
<Z>-258</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Holder>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>-37</X>
<Y>0</Y>
<Z>-293</Z>
</DrivenPoint>
<DrivenPoint ID="2">
<Type>-1</Type>
<Radius>0</Radius>
<X>-37</X>
<Y>0</Y>
<Z>-293</Z>
</DrivenPoint>
<CutterCompensation ID="1">1.2</CutterCompensation>
<CutterCompensation ID="2">1.2</CutterCompensation>
</Tool>
<Tool ID="100178" Units="Millimeter">
<Description>100178</Description>
<Teeth>8</Teeth>
<Type>Milling</Type>
<Cutter>
<Reference ID="10084 - R90MT-12-05-RM">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>44</X>
<Y>0</Y>
<Z>-614</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM1">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>31.112698</X>
<Y>-31.112698</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>-45</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM11">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>31.112698</X>
<Y>31.112698</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>45</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM12">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>0</X>
<Y>-44</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>-90</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM13">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>0</X>
<Y>44</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>90</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM14">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>-31.112698</X>
<Y>31.112698</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>135</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM15">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>-44</X>
<Y>0</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>180</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10084 - R90MT-12-05-RM">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10084 - R90MT-12-05-RM</EntityName>
<Origin>
<X>-31.1127</X>
<Y>-31.1127</Y>
<Z>-614</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>-135</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Cutter>
<Holder>
<Reference ID="11974 - C8-390.410-100 120A">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>11974 - C8-390.410-100 120A</EntityName>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="12001 - C8-391.01-80 065">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12001 - C8-391.01-80 065</EntityName>
<Origin>
<X>0</X>
<Y>0</Y>
<Z>-120</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="12001 - C8-391.01-80 065">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12001 - C8-391.01-80 065</EntityName>
<Origin>
<X>0</X>
<Y>0</Y>
<Z>-185</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="12002 - C8-391.05-CD-32-320">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12002 - C8-391.05-CD-32-320</EntityName>
<Origin>
<X>0</X>
<Y>0</Y>
<Z>-250</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10707 - FCM-D100-32-11">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>10707 - FCM-D100-32-11</EntityName>
<Origin>
<X>0</X>
<Y>0</Y>
<Z>-570</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Holder>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-620</Z>
</DrivenPoint>
<DrivenPoint ID="2">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-620</Z>
</DrivenPoint>
<CutterCompensation ID="1">50</CutterCompensation>
<CutterCompensation ID="2">50</CutterCompensation>
</Tool>
<Tool ID="100192" Units="Millimeter">
<Description>100192</Description>
<Teeth>1</Teeth>
<Type>Milling</Type>
<Cutter>
<Reference ID="10087 - 880-05-03-05H-C-GM">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10087 - 880-05-03-05H-C-GM</EntityName>
<Origin>
<X>-7.9</X>
<Y>0</Y>
<Z>-233.5</Z>
</Origin>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>180</Z>
</Rotation>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="10088 - 880-05 03 W05H-P-GM">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Rotativas - Insertos Paramétricos</ToolID>
<EntityName>10088 - 880-05 03 W05H-P-GM</EntityName>
<Origin>
<X>12</X>
<Y>0</Y>
<Z>-233.5</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Cutter>
<Holder>
<Reference ID="12007 - C4-390.410-100-090A">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12007 - C4-390.410-100-090A</EntityName>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="12033 - 880-D2500C4-04">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12033 - 880-D2500C4-04</EntityName>
<Origin>
<X>0</X>
<Y>0</Y>
<Z>-90</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Holder>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-234</Z>
</DrivenPoint>
</Tool>
<Tool ID="801154" Units="Millimeter">
<Description>801154</Description>
<Teeth>1</Teeth>
<Type>Turning</Type>
<Cutter>
<Reference ID="11325 - MKI-176807-13_4.4">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Torneamento - Insertos 3D</ToolID>
<EntityName>11325 - MKI-176807-13_4.4</EntityName>
<Origin>
<X>-5.18</X>
<Y>0</Y>
<Z>-225</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Cutter>
<Holder>
<Reference ID="12016 - 392.410-100-ASHL-32">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>12016 - 392.410-100-ASHL-32</EntityName>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
<Reference ID="11328 - MKS-366807-18">
<FileName>TBT_MASTER_LIBRARY.tls</FileName>
<ToolID>Holders & Acessorios</ToolID>
<EntityName>11328 - MKS-366807-18</EntityName>
<Origin>
<X>-8</X>
<Y>0</Y>
<Z>-55</Z>
</Origin>
<NoSpin>0</NoSpin>
<Alternate>Off</Alternate>
</Reference>
</Holder>
<DrivenPoint ID="2">
<Type>-1</Type>
<Radius>0</Radius>
<X>-2.980037</X>
<Y>0</Y>
<Z>-225</Z>
</DrivenPoint>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>-7.380037</X>
<Y>0</Y>
<Z>-225</Z>
</DrivenPoint>
<CutterCompensation ID="1">1.5</CutterCompensation>
<CutterCompensation ID="2">1.5</CutterCompensation>
</Tool>
</Tools>
</ToolLibrary>
What do I have to do in the code above (Considering it´s the right code - Feel free to come up with a new approach) to make my TreeView to be populated like shown in the image?
I´m sorry for asking for a thing possibly already covered, I did a search and could not find something that could work with my XML example.
Tks in advance,
Daniel
I improved the output. See code below