Create an editable XML document (one) to bind an XML document to the TreeView control

Source: Internet
Author: User
Tags add bind contains end interface net object model string
treeview|xml| Create | control

Directory:

Introduced

Binding an XML document to the TreeView control

Filtering XML data

To perform a drag-and-drop operation

Perform delete, rename, insert operation

Using the TreeView control in

With XML and XPath you can add drag-and-drop even more functionality to your TreeView control without any effort-by Alex Hildyard

Recently, I've been developing a user interface tool to maintain an online catalog, because it contains too many products, so it makes sense to classify them in a number of ways. Directory administrators will need to have the ability to delete and define new directories, to nest between directories and directories, and to make directories and products look intuitive in a clever way.

A classified scenario like this desperately needs a hierarchical view of sorts, first: the mapping between data and its representation is usually trivial (trivial) because the object model of the TreeView control is hierarchical. Second: The ability to expand a separate tree node makes it easier to browse through data at multiple levels. Finally: dragging and dropping folders in the TreeView is a very simple and noticeable way to quickly handle complex hierarchies.

A few minutes later, I realized that the application in my head was Windows Explorer (Windows Explorer), and I wanted to rewrite it, replace the folder with the product catalog, replace the file with the product project, and even quickly implement such operations as creating or deleting folders, dragging and dropping. If I later write an interface for a relational data, or write a contact management program, or develop a tool to track my family genealogy, I will find that I do the same thing.

There's no point in doing this, I need to find a common way to provide a hierarchical data source for the TreeView control, like creating a data table (database table) for a data grid control in the databases, and making it easy to create, The ability to delete, rename, move, and drag data elements without having to worry about the structure of the data source content in the query

To create an XML document for the TreeView control:

According to the TreeView hierarchy, XML is a very logical data format, you can display XML documents in the TreeView control with less than 6 lines of code, assuming you have an XML document similar to the following, which contains many contact nodes:

<?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 assemble all the data elements into the TreeView control by recursive invocation, and after adding all the XML document nodes to the TreeView, you can maintain the node relationship of the maintenance XML document by maintaining the TreeView control
[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 Form, drag and drop a TreeView control onto the form, and add the following three lines to your data file:
[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 node, you will see the contents of the Tuyi:





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.