执行删除、改名、插入操作 实现了拖放操作就已经完了最难的部分,但是出于完整性考虑,还应该提供一些更好的基本的编辑功能。下面仅仅用四行代码就可以实现删除操作: [C#] string xpath_query = buildXPathQuery(this.SelectedNode); System.Xml.XmlNode node = xml_document.DocumentElement.SelectSingleNode( xpath_query); node.ParentNode.RemoveChild(node); SelectedNode.Remove();
[VB] Dim xpath_query As String = _ buildXPathQuery(Me.SelectedNode) Dim node As System.Xml.XmlNode = _ xml_document.DocumentElement.SelectSingleNode( _ xpath_query) node.ParentNode.RemoveChild(node) SelectedNode.Remove() 重命名操作需要更多的一些考虑,你可以调用buildXPathQuery 去查找那个文件夹正在被编辑,还有如何确定是哪一个属性,如何确定被显示名称相应的返回结构的元素或者子元素?这是一个误导的问题,XPath filte函数已经被定义了,你只需应用现面的转换就可以了: [C#] private void XmlTreeView_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) { string xpath_query = buildXPathQuery(e.Node); System.Xml.XmlNode node = xml_document.DocumentElement.SelectSingleNode( xpath_query); System.Xml.XmlNode label = node.SelectSingleNode(xpath_filter); label.Value = e.Label; }
[VB] Private Sub XmlTreeView_AfterLabelEdit( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) _ Handles MyBase.AfterLabelEdit Dim xpath_query As String = buildXPathQuery(e.Node) Dim node As System.Xml.XmlNode = _ xml_document.DocumentElement.SelectSingleNode( _ xpath_query) Dim label As System.Xml.XmlNode = node.SelectSingleNode(xpath_filter) label.Value = e.Label End Sub 最后一个挑战就是如何按照需求创建一个新的文件夹。query filter 允许用户用一组文件夹而不是xml元素操作xml文档。这样对你想在那些文件夹周围进行移动很有帮助的,而且它还可以告诉在哪里插入一个文件夹,但是它不能告诉你这个文件夹应该包含什么东西,你应该猜测文档中的所有文件夹是否含有相同的结构或者内容。但是我们的目标是创建一个不需要任何预定的显示xml 方法。因此,我选择将这些怎人委托给应用程序控制。例如,客户端代码可以下面这样写: [C#] System.Xml.XmlDocument insert_fragment = new System.Xml.XmlDocument(); insert_fragment.LoadXml( "<product id='New Item'>" + "<description/><ordercode/><price/>" + "<image/></product>"); // The TreeView uses XmlInsertionNode to add // a new folder to the tree's underlying XML // document on request xmlTreeView1.XmlInsertionNode = insert_fragment.DocumentElement;
[VB] Dim insert_fragment As New System.Xml.XmlDocument() insert_fragment.LoadXml(" & _ "<product id='New Item'>" & _ "<description/><ordercode/>"<price/>" & _ "<image/></product>") xmlTreeView1.XmlInsertionNode = _ insert_fragment.DocumentElement treeview 控件可以缓存一个结构的副本,将它作为一个临时变量来创建一个新的文件夹集合。你所要做的仅仅是确保被定义得文件夹可以被filter query 识别,否则treeview 将不能显示它。因为对于我们操作的文档,xml通常是外部的(external)。在我使用它之前需要导入到文档中 [C#] // First you need to clone the node template, and // import it, because it originates from a different // document System.Xml.XmlNode copy_node = new_node.Clone(); System.Xml.XmlNode insert_node = xml_document.ImportNode(copy_node, true); // Next locate which node should be its parent, and // insert it string xpath_query = buildXPathQuery(this.SelectedNode); System.Xml.XmlNode node = xml_document.DocumentElement.SelectSingleNode( xpath_query); node.AppendChild(insert_node); // Finally, apply the xpath filter to determine what // to display System.Xml.XmlNode expr = insert_node.SelectSingleNode(xpath_filter); System.Windows.Forms.TreeNode new_child = SelectedNode.Nodes.Add(expr.Value); populateTreeControl(insert_node, new_child.Nodes); // Select the node, to force the tree to expand SelectedNode = new_child; // And start editing the new folder name suppress_label_edit = false; new_child.BeginEdit();
[VB] ' First you need to clone the node template, and ' import it, because it originates from a different ' document. Dim copy_node As System.Xml.XmlNode = new_node.Clone() Dim insert_node As System.Xml.XmlNode = _ xml_document.ImportNode(copy_node, True) ' Next locate which node should be its parent, ' and insert it Dim xpath_query As String = _ buildXPathQuery(Me.SelectedNode) Dim node As System.Xml.XmlNode = xml_document.DocumentElement.SelectSingleNode( _ xpath_query) node.AppendChild(insert_node) ' Finally, apply the xpath filter to determine what ' should be ' displayed Dim expr As System.Xml.XmlNode = _ insert_node.SelectSingleNode(xpath_filter) Dim new_child As System.Windows.Forms.TreeNode = _ SelectedNode.Nodes.Add(expr.Value) populateTreeControl(insert_node, new_child.Nodes) ' Select the node, to force the tree to expand SelectedNode = new_child ' And start editing the new folder name suppress_label_edit = False new_child.BeginEdit()
|