Create an editable XML document (bis) Filter XML data

Source: Internet
Author: User
Tags add filter end query string xpath
xml| Create | data

Filtering XML data:

Let's assume that if in a real-world contact application, users may not want to see "email", "City" or "country" in the hierarchical TreeView, they may prefer to see top user contact identities, such as Alex, Rebekah, Or Justin, since the corresponding details of the contact point (email,city) are in the adjacent editable areas, similar users may want to rearrange them by dragging the tree nodes up and down, but moving the email through the TreeView control within the individual contact points The address or city is meaningless. People usually want to organize the data in a hierarchical view, rather than simply classifying them, it is normal to reorder the contacts (contact), unless the city and country elements are clearly associated with the specified connection, and you may want to deal with them individually, Instead of grouping them into pairs.

A good solution is to hide the child nodes when the contact tree is displayed, for example, you can email. Add a special attribute (such as view= "Hide") to a child element that you do not want to display, so that you can omit any element (including their child nodes) by setting this special property in the assembly method when you assemble the tree control, which can be used to work But changing the data source to fit the user's display is not a reliable design idea

A better idea is to define the structure of a hierarchical view for data clients within a given document, and you can modify the Populatetreecontrol () method to support XPath for example:

[C#]
   private void populateTreeControl(System.Xml.XmlNode document,
      System.Windows.Forms.TreeNodeCollection nodes)
   {
      foreach (System.Xml.XmlNode node in 
         document.ChildNodes)
      {
         System.Xml.XmlNode expr = 
            node.SelectSingleNode(xpath_filter);
         
         if (expr != null)
         {
            TreeNode new_child = new 
               TreeNode(expr.Value);
            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
         Dim expr As System.Xml.XmlNode =  _
            node.SelectSingleNode(xpath_filter)
   
         If Not (expr Is Nothing) Then
            Dim new_child As New TreeNode(expr.Value)
            nodes.Add(new_child)
            populateTreeControl(node, new_child.Nodes)
         End If
      Next
   End Sub
Add the following line to the class-level scope:
[C#]
   private string xpath_filter = 
      "@id[parent::contacts or parent::contact]";
   
   [VB]
   Private xpath_filter As String = _
      "@id[parent::contacts or parent::contact]"
You can use the results returned by the XPath query to decide whether to recursively call the Mount Subnode, which establishes a containing rule that reads "Select the id" of any ' contacts ' or ' contact ' element. ' You can also use an exclusion rule to determine which data you need to reject.
attribute::id[not(parent::email or 
      parent::city or parent::country)]

This is not a common solution, but a parent-child based filter is much better than a node or attribute based on no restrictions, which is an effective way to express the basic structure of an XML document when the user has sufficient editable permissions without interfering with its hierarchy. Since such a simple query is sufficient, unless you need to do more complex operations



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.