Create an editable XML document (iv) Delete, rename, insert operation

Source: Internet
Author: User
xml| Insert | create
Perform a delete, rename, insert operation
Dragging and dropping is the hardest part of implementing a drag-and-drop operation, but it should also provide some of the better basic editing features for integrity reasons.
The following four lines of code can be used to implement the delete operation:
[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()
Renaming operations requires a little more consideration, you can call   to find out which folder is being edited, and how to determine which attribute, how to determine the corresponding return structure of the display name elements or child elements? This is a misleading question, and the XPath filte function has been defined, and you can simply apply the current surface transformation:
[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
The final challenge is to create a new folder as needed. query filter allows users to manipulate XML documents with a set of folders instead of XML elements. This is helpful for you to move around those folders, and it can tell you where to insert a folder, but it doesn't tell you what the folder should contain, and you should guess if all the folders in the document contain the same structure or content. But our goal is to create a display XML method that does not require any scheduling. So I chose to delegate these to application control. For example, client code can write as follows:   
[C#]
   System.Xml.XmlDocument insert_fragment = 
      new System.Xml.XmlDocument();
   insert_fragment.LoadXml(
      " 
    
      " + 
    
      " 
     
     
    " + 
      "");
      
   // 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(" & _
      " 
    
      " & _ 
    
      " 
     
    " 
    " & _
      "")
   xmlTreeView1.XmlInsertionNode = _
      insert_fragment.DocumentElement
The TreeView control can cache a copy of a structure and create a new collection of folders as a temporary variable. All you have to do is make sure that the folder you defined is identified by filter query, otherwise the TreeView will not be able to display it. Because for the documents we operate, XML is usually external (external). I need to import it into my document before I use it 
   [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()


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.