Question Load XML schema elements to tree view box

anil3b2

Member
Joined
Jun 19, 2009
Messages
14
Programming Experience
1-3
Hi,

I have a xml schema file which is having all elements list and details etc., I need to load this xml schema elements to tree view tool box like how the xml schema loaded in word xml structure..

Or please suggest any other way to deal with this.

Thanks in advance
Anil
 
Obviously you can't include the 'end node' tag, the Treeview control does not have such design, but all node names can be added in tree style.

How is a root node added? TreeView.Nodes.Add
How is a child node added? TreeNode.Nodes.Add
"Nodes" here is in both cases of type TreeNodeCollection.

How about the xml? XmlDocument already contains a tree structure where each element is a XmlNode and has a ChildNodes collection of type XmlNodeList. So you can start by reading the root element (DocumentElement) and continue to read childs until all nodes is processed.

What I'm getting at is all the requirements exist to make a recursive method, a method that call itself with same type of parameters, where for each call the parameters contains a new level of information. Example code:
VB.NET:
Private Sub LoadTree(ByVal xnode As Xml.XmlNode, ByVal tnodes As TreeNodeCollection)
    Dim tnode As TreeNode = tnodes.Add(xnode.Name)
    For Each child As Xml.XmlNode In xnode.ChildNodes
        LoadTree(child, tnode.Nodes)
    Next
End Sub
As you can see this method adds a TreeNode from the specified XmlNode name, then calls itself to load the ChildNodes to that TreeNode. To load the xml document and call this method:
VB.NET:
Dim doc As New Xml.XmlDocument
doc.Load("test.xsd")
LoadTree(doc.DocumentElement, Me.TreeView1.Nodes)
Me.TreeView1.ExpandAll()
 
Back
Top