Create an editable xml document (II) to filter xml data

Source: Internet
Author: User
Filter xml data:

Let's assume that in a real contact application, users may not want to see "email", "city" or "country" in the hierarchical treeview ", they may want to see the top-level user contact identity, such as Alex, Rebekah, or Justin. Now, the details of the contact point (email, city) it is in an adjacent editable area. Similar users may wish to rearrange the nodes by dragging up or down the tree, however, it is meaningless to move the email address or city through the treeview control within an individual contact point. People usually want to use a hierarchical view to organize data, instead of simply classifying them. In other words, it is normal to resort and group contacts, unless the city and country elements are clearly associated with the specified contact, and you may want to process them separately rather than grouping them into pairs.

A good solution is to hide sub-nodes when the contact tree is displayed. For example, you can use email. add a special attribute (for example, view = "hide") to the child element that you do not want to display, such as Address. In this way, you can set this special attribute in the assembly method when assembling the tree control, this allows you to ignore any elements (including their subnodes). Although this can work, changing the data source to suit User display is not a very reliable design concept.

A better idea is to define the hierarchical view visible structure for Data customers within a given document. You can modify the populateTreeControl () method to make it 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
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 row to the class level range:

[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 result returned by the xpath query to determine whether to recursively call and load the subnode. This query creates a inclusion rule, read the "Select the id attribute of any 'contacts' or 'Contact 'element. ", you can also use an exclusion rule to determine which data you want to reject.

Attribute: id [not (parent: email or
Parent: city or parent: country)]
This is not a general solution, but filtering based on parent-child relationship is much better than Filtering Based on nodes or attributes without restrictions, this is an effective way to express the basic structure of an xml document when a user has sufficient editable permissions without interfering with its hierarchy. Since such a simple query is enough, unless you need to perform 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.