Create an editable xml document (4) delete, rename, and insert operations

Source: Internet
Author: User
Delete, rename, and insert operations
The most difficult part has been completed after the drag-and-drop operation is implemented, but some better basic editing functions should be provided for the sake of integrity. The following code uses only four lines to delete an object:
[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 ()
The RENAME operation requires more consideration. You can call buildXPathQuery to find the folder being edited and determine the attribute, how do I determine the elements or child elements of the returned structure corresponding to the displayed name? This is a misleading problem. The XPath filte function has been defined. You only need to apply the current conversion:
[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
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 last challenge is how to create a new folder as needed. Query filter allows users to operate xml documents with a group of folders instead of xml elements. This is helpful for moving around those folders, and it can also tell you where to insert a folder, but it cannot tell you what the folder should contain, you should guess whether all folders in the document contain the same structure or content. However, our goal is to create an xml display method that does not require any reservation. Therefore, I chose how to delegate these to application control. For example, the client code can be written as follows:
[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
The treeview control can cache a copy of the structure and use it as a temporary variable to create a new folder set. All you have to do is ensure that the defined folder can be recognized by the filter query, otherwise the treeview will not display it. Because xml is usually external for the documents we operate on ). Before using it, you need to import it to the document.

[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 shoshould 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 shoshould 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
'Could 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.