Data Source binding of Treeview-use XML as the data source to add, delete, modify, and query the Treeview, and then write back the XML document.

Source: Internet
Author: User

Some time ago, the Treeview control was used in the project (C #) winfrom). At the beginning, I thought of loading the data source using XML, after reading msdn for a few days (the Treeview control and XML were not used in the past), I started to writeCodeBut XML is very complicated, but the basic addition, deletion, modification, and query are still similar.

Now, paste the code I have written step by step.

I first wrote a function to call the xmldocument to facilitate other calls to the xmldocument, such as addition, deletion, modification, and query.

         ///   <Summary>  
/// Create an XML Function
/// </Summary>
/// <Returns> </returns>
Private Xmldocument getpath ()
{
Xmldocument xmldoc = New Xmldocument ();
String Path = appdomain. currentdomain. basedirectory + " Areaname. xml " ;
Xmldoc. Load (PATH );
Return Xmldoc;
}

When initializing the winfrom form, load the following functions. -- Query function, bind the XML document data to the Treeview, and generate a node tree.

         ///   <Summary>  
/// Bind XML to Treeview
/// </Summary>
Private Void Addtreeview ()
{
Try
{
Xmldocument xmldoc = getpath ();
// Disable repainting
Tree_area.beginupdate ();
Xmlelement Xe = (xmlelement) xmldoc. selectsinglenode ( " Root " );
// Bind an attribute node to the Treeview
Treenode root = New Treenode ();
Root. Name = Xe. getattribute ( " Name " );
Root. Text = Xe. getattribute ( " Value " );
Root = getchildnodes (xe, root );
Tree_area.nodes.add (Root );
Tree_area.endupdate ();
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}

/// <Summary>
/// Recursively traverse nodes
/// </Summary>
/// <Param name = "XE"> </param>
/// <Param name = "upnode"> </param>
/// <Returns> </returns>
Private Treenode getchildnodes (xmlelement XE, treenode upnode)
{
// Recursively traverse nodes
If (Xe. haschildnodes)
{
Foreach (Xmlnode Node In Xe. childnodes)
{
Xmlelement xlt = (xmlelement) node;
Treenode Tn = New Treenode ();
Tn. Text = xlt. getattribute ( " Name " );
Tn. Name = xlt. getattribute ( " Value " );
Getchildnodes (xlt, TN );

Upnode. nodes. Add (TN );

}
}
Return Upnode;
}

Then, two textbox controls are added to the winfrom interface to receive data from the controls that the user needs to add to the Treeview.

Add -- add data to the Treeview control.

         Private   Void Insertnode ()
{
Treenode selectnode = Null ;
Treenode node = Null ;
Xmldocument xmldoc = getpath ();
Selectnode = This . Tree_area.selectednode;
Node = New Treenode ();

Node. Text = txt_nub.text.trim ();
Node. Name = txt_areaname.text.trim ();
If (Txt_areaname.text! = "" & Txt_nub.text! = "" )
{
If (Txt_areaname.text = "" & Txt_nub.text = "" )
{
Tree_area.nodes.add (node );
}
Else
{
Treenode ttn = New Treenode ();
Ttn. Name = txt_nub.text;
Ttn. Text = txt_nub.text;
Tree_area.nodes.add (ttn );
}
}
Else
{
MessageBox. Show ( " Select a node or text box. It cannot be blank! " );
}

}

Remove -- delete a node of the Treeview control.

         Private   Void Deletenode ()
{
Xmldocument xmldoc = getpath ();

If (Txt_areaname.text.trim ()! = "" & Txt_nub.text.trim ()! = "" )
{
Treenode selectnode = tree_area.selectednode;
If (Selectnode! = Null )
{
Treenode parentnode = selectnode. parent;

If (Parentnode = Null )
{
// Delete the currently selected node from the node set in the Treeview.
Tree_area.nodes.remove (selectnode );
Txt_areaname.text ="" ;
Txt_nub.text = "" ;
}
Else
{
// Deletes the selected node from the parent node set.
Tree_area.nodes.remove (selectnode );
Txt_nub.text = "" ;
Txt_areaname.text = "" ;
Xmlnode root = xmldoc. selectsinglenode ( " Root " );
Xmlnodelist xnl = xmldoc. selectsinglenode ( " Root " ). Childnodes;
For ( Int I = 0 ; I <xnl. Count; I ++)
{
Xmlelement Xe = (xmlelement) xnl. item (I );
If (Xe. getattribute ( " Name " ) = Selectnode. Name)
{
Root. removechild (xe );
If (I <xnl. Count)
{
I = I- 1 ;
}
}
}
}
}
Else
{
MessageBox. Show ( " The node is not selected or the parent node cannot be deleted! " , " Prompt " );
}
}
}

Alter -- edit the data of the Treeview control.

         Private   Void Updatenode ()
{
If (Txt_areaname.text.trim ()! = "" & Txt_nub.text.trim ()! = "" )
{
Treenode selectnode = tree_area.selectednode;
Selectnode. Text = txt_areaname.text.trim ();
Selectnode. Name = txt_nub.text.trim ();
Txt_areaname.text = selectnode. text;
Txt_nub.text = selectnode. Name;
Txt_areaname.text = "" ;
Txt_nub.text = "" ;
}
Else
{
MessageBox. Show ( " Select a node or text box. It cannot be blank! " );
}

}

The preceding function adds, deletes, modifies, and queries the Treeview control on the winfrom form, but this step is not enough, it only implements addition, deletion, modification, and query on the Treeview control. But as mentioned earlier, you also need to save the modified Treeview data to the XML document.

The following function writes the data of the Treeview control to the XML document.
First, you must define the version and format of the XML document.

Xmltextwriter Tw =Null ;
/// <Summary>
/// Write the Treeview node to the XML document.
/// </Summary>
Private Void Addxml ()
{
String Filename = appdomain. currentdomain. basedirectory + " Areaname. xml " ;
Tw = New Xmltextwriter (filename, Null );

// Set the format of the written XML document to indent
Tw. Formatting = formatting. indented;

// Declare default version 1.0
Tw. writestartdocument ();

// Current retrieved Node
Writexml (tree_area.nodes );

// Close open Element
Tw. writeenddocument ();

// Refresh basic stream
Tw. Flush ();
Tw. Close ();
}

/// <Summary>
/// Recursively write the current Treeview node into the XML document
/// </Summary>
/// <Param name = "Tc"> </param>
Public Void Writexml (treenodecollection TC)
{
Foreach (Treenode Node In TC)
{
Writeonexml (node );
Writexml (node. nodes );

// Disable used nodes
Tw. writeendelement ();
}
}
         ///   <Summary>  
/// Layout element nodes and attribute nodes
/// </Summary>
/// <Param name = "Node"> Treeview Node </Param>
Private Void Writeonexml (treenode node)
{
String Value = node. Name;
String Name = node. text;
// Write element Nodes Based on Treeview nodes
Switch (Node. Level)
{
Case 0 :
Tw. writestartelement ( " Root " );
Break ;
Case 1 :
Tw. writestartelement ( " Province " );
Break ;
Case 2 :
Tw. writestartelement ( " City " );
Break ;
Case 3 :
Tw. writestartelement ( " Piecearea " );
Break ;
Case 4 :
Tw. writestartelement ( " Town " );
Break ;
Case 5 :
Tw. writestartelement ( " Equipmentn " );
Break ;
}

// Write attribute node
Tw. writeattributestring ( " Value " , Value );
Tw. writeattributestring ( " Name " , Name );
// ... Remaining attribute nodes

}

After running,

Written at the end: I wrote the above based on my own project, but the overall implementation is still: 1. read the XML document and bind it to the Treeview control. 2. edit the data of the Treeview control on the form and return the XML document.

However, in my current project, XML is used as the data source. After editing the Treeview node and uploading it back to the XML document, there are still some problems because the format of my XML document is more complex. In addition, this source is used in multiple places. Therefore, after data is added to a certain place, synchronization is required. Although this can be achieved through inheritance of classes, however, the current time is not allowed. So you have to use other methods. Currently, SQLite is used as a lightweight database. After reading the materials on the official website, It is very convenient to use them. If the stored data volume is less than 10 million, it is good to use. The most important thing is that, most of the current mainstream database syntaxes can be used in SQLite.

I will also write an article about using SQLite as the data source of the Treeview control later.Article. You are welcome to leave a message or send an email for discussion. Thank you! Via cnblogs.com/aehoo/ 2012.02.25

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.