Create an editable XML document (one) bind the XML document to the TreeView control

zhaozj2021-02-17  57

table of Contents:

Introduction

Bind XML document to the TreeView control

Filter XML data

Execute drag and drop operation

Perform deletion, rename, insert operation

TreeView control in use

With XML and XPath, you can add drag and so more functions for your TreeView controls - BY Alex Hildyard

Recently, I have been developing a user interface tool for maintaining online catalog, because this directory contains too many products, so it is very meaningful to use some ways. Directory administrators will need to delete and define new directory capabilities,> The ability to nested between records and directorys, and use in a smart way to make directory and product look intuitive.

The classification plot is urgently needed to be classified according to the type of classification, first: mapping between data and its representation is usually slightly insignificant, because the TreeView control object model is self-level. Second: The ability to expand a separate tree node will be easier to browse data with multiple levels. Finally: Drag and drop folders in TreeView are quickly handled complicated hierarchical methods.

After a few minutes, I realized that the app in my mind is Windows Explorer (Windows Explorer), and I have to rewrite it, replace the folder with the product catalog, replace files with product projects, and even I can implement quickly Similar to create or delete folders, perform drag and other operations. If I have written interfaces for a relational data, or write a contact management program, or develop a tool to track my family score, then I will find that I am doing the same thing.

It is meaningless to find this, I need to find a general method for providing a hierarchical data source for the TreeView control, which is like a Data Grid to create a data table (DATABASE TABLE) in the database, and It can easily create, delete, rename, move, and drag and drop the function of data elements without the need for the structure of data source content in the question.

Create an XML document for the TreeView control:

According to the TreeView hierarchy, XML is a very logical data format, you can use less than 6 lines of code to display XML documents in the TreeView control, assume you have an XML document similar to the following, which contains a lot of contacts ( Contact Node:

Someone@some_pop_mail.net

Edinburgh

United Kingdom

Someone@some_web_mail.net

Papakura

New Zealand

Someone_ELSE@some_web_mail.com

muriwai

New Zealand

You can easily assemble all the data elements into the TreeView control by recursive calls, you can maintain all the XML documents to maintain the Node relationship of the XML document by maintaining the TreeView control.

[C #]

Private void PopulaTreeControl

System.xml.xmlNode Document,

System.Windows.Forms.treenodeCollection Nodes)

{

Foreach (System.xml.xmlnode Node in

Document.childNodes)

{

// if the element has a value, display it;

// Otherwise Display the First Attribute

// (if there is one) or the element name

// (if there isn't)

String text = (node.value! = null? node.value:

(Node.attributes! = null&&

node.attributes.count> 0)?

Node.attributes [0] .value: node.name);

Treenode new_child = new treenode (text);

Nodes.add (new_child);

PopulaTreeControl (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

'Ness the element has a value, display it;

'Otherwise Display THE FIRST ATTRIBUTE

'(if there is one) or the element name

'(if there isn't)

DIM [Text] as string

IF node.value <> Nothing then

[text] = node.value

Else

If not node.attributes is nothing and _

Node.attributes.count> 0 THEN

[text] = node.attributes (0) .value

Else

[text] = node.name

END IF

END IF

Dim new_child as new treenode ([text])

Nodes.add (new_child)

PopulatetreeControl (Node, New_Child.nodes) Next Node

End Sub

Now, you can create a new Windows form, drag and drop a TreeView control to the form, add the following three lines into your data file:

[C #]

System.xml.xmldocument Document =

New system.xml.xmldataDocument ();

Document.Load ("../../undacts.xml");

PopulaTreeControl (Document.Documentelement,

TreeView1.nodes;

[Vb]

DIM Document As new system.xml.xmldatAdocument ()

Document.Load ("../ contacts.xml")

PopulaTreeControl (Document.documentelement, _

TreeView1.nodes)

When you expand the node of TreeView, you will see the contents of the picture:

转载请注明原文地址:https://www.9cbs.com/read-30365.html

New Post(0)