Basically, I am looking to create a TreeView structure from my XML file.
Here is the XML file: http://pastebin.com/gvTzfbgE
I am using this code, slightly modified to display different text, from the MSDN pages:
Private Sub btnProcess_Click(sender As Object, e As EventArgs) Handles btnProcess.Click Try ' SECTION 1. Create a DOM Document and load the XML data into it. Dim dom As New XmlDocument() dom.Load(fDirectory.FileName) ' SECTION 2. Initialize the treeview control. TreeView1.Nodes.Clear() TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name)) Dim tNode As New TreeNode() tNode = TreeView1.Nodes(0) ' SECTION 3. Populate the TreeView with the DOM nodes. AddNode(dom.DocumentElement, tNode) 'TreeView1.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 ' 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 As Short = 0 To nodeList.Count - 1 xNode = inXmlNode.ChildNodes(i) inTreeNode.Nodes.Add(New TreeNode(xNode.Name)) 'GetStringBetween(xNode.InnerXml, "TMNode Name=""", """"))) tNode = inTreeNode.Nodes(i) inTreeNode.Nodes(i).Text = GetStringBetween(xNode.InnerXml, "TMNode Name=""", """") 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. 'MsgBox((inXmlNode.OuterXml).Trim) inTreeNode.Text = (inXmlNode.OuterXml).Trim End If End Sub The result I am getting is this:
...but if you've viewed the XML file, "Part-Press" should be one of seven children. This code is only displaying the first and then everything just messes up.
The result I want would be:
VisiTM -Assembly --Part-Press --Shrink --Water Subs --CORE HALF --CAVITY HALF --COMPONENTS AT FULL TRAVEL --Water Offset And of course, the right children in each child. Here is an image of the XML file in web browser, which should show what I am aiming for:
(The TreeView will be used to create a folder system that will replicate the XML file, using the values for folder names. I figured porting the XML file into TreeView and then parsing that will work best, unless someone has a different approach than using a TreeView.)
Thank you
No comments:
Post a Comment