Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create Editable XML Documents Using XPath and the TreeView Control : Page 2

Adding drag and drop support to your TreeView control needn't be taxing. With XML and Xpath, you can get most of that support for free.


advertisement
Binding an XML Document to a TreeView Control
With its hierarchical structure, XML was the logical choice of data format. You can display an XML document in a TreeView control in fewer than half-dozen lines of code.

Suppose you have an XML document containing contacts like this (contacts.xml):

<?xml version="1.0" encoding="utf-8"?> <addressbook> <contacts id="Contacts"> <contact id="Alex"> <email id="popmail"> someone@some_pop_mail.net</email> <city>Edinburgh</city> <country>United Kingdom</country> </contact> <contact id="Rebekah"> <email id="webmail"> someone@some_web_mail.net</email> <city>Papakura</city> <country>New Zealand</country> </contact> <contact id="Justin"> <email id="webmail"> someone_else@some_web_mail.com</email> <city>Muriwai</city> <country>New Zealand</country> </contact> </contacts> </addressbook>

You can easily iterate recursively through all its elements to populate the TreeView control, adding the XML nodes to the TreeView's TreeNodeCollection while maintaining the same node relationship that exists in the XML document.

[C#] private void populateTreeControl( System.Xml.XmlNode document, System.Windows.Forms.TreeNodeCollection nodes) { foreach (System.Xml.XmlNode node in document.ChildNodes) { // If the element has a value, display it; // otherwise display the first attribute // (if there is one) or the element name // (if there isn't) string text = (node.Value != null ? node.Value : (node.Attributes != null && node.Attributes.Count > 0) ? node.Attributes[0].Value : node.Name); TreeNode new_child = new TreeNode(text); nodes.Add(new_child); populateTreeControl(node, new_child.Nodes); } }




[VB] Private Sub populateTreeControl( _ ByVal document As System.Xml.XmlNode, _ ByVal nodes As _ System.Windows.Forms.TreeNodeCollection) Dim node As System.Xml.XmlNode For Each node In document.ChildNodes ' If the element has a value, display it; ' otherwise display the first attribute ' (if there is one) or the element name ' (if there isn't) Dim [text] As String If node.Value <> Nothing Then [text] = node.Value Else If Not node.Attributes Is Nothing And _ node.Attributes.Count > 0 Then [text] = node.Attributes(0).Value Else [text] = node.Name End If End If Dim new_child As New TreeNode([text]) nodes.Add(new_child) populateTreeControl(node, new_child.Nodes) Next node End Sub

Now, you can create a new Windows Forms application, drop a TreeView control onto the form, and add the following three lines to your form's constructor:

[C#] System.Xml.XmlDocument document = new System.Xml.XmlDataDocument(); document.Load("../../contacts.xml"); populateTreeControl(document.DocumentElement, treeView1.Nodes);


[VB] Dim document As New System.Xml.XmlDataDocument() document.Load("../contacts.xml") populateTreeControl(document.DocumentElement, _ TreeView1.Nodes)

When you expand the TreeView nodes, you should see something like Figure 1.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date