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