Two times of system installation at home these two days (depressing ..), Now the problem of not connecting to the ADSL Network is finally solved (it turns out to be a problem with the NIC Driver, I am dizzy )!! No more nonsense. To bind a Treeview to an XML document, you only need to write a recursive process. The XML Code is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
<! -- Note: Treeview is the root node, nodes is the parent node, and node is the child node.
-->
< Treeview >
< Nodes >
< Name > Root Node 1 </ Name >
< Zuozhe />
< Liupai />
< Link />
< Node >
< Name > Test Node 1-1 </ Name >
< Zuozhe />
< Liupai />
</ Node >
< Node >
< Name > Test Node 1-2 </ Name >
< Zuozhe />
< Liupai />
< Link />
</ Node >
</ Nodes >
Add a Treeview control and a button control to the page. Write a recursion process as follows:
/// < Summary >
/// Build the tree. Traverse all nodes in XML
/// </ Summary >
/// < Param Name = "Xmlnodes" > XML Node </ Param >
/// < Param Name = "Treenode" > Current node of the tree </ Param >
Private void buildtree (xmlnodelist xmlnodes, treenodecollection treenodes)
{
String svalue;
For (INT I = 0; I < Xmlnodes . Count; I ++)
{
If (xmlnodes [I]. nodetype = Xmlnodetype. element)
{
Treenode tnode = New Treenode ();
Svalue = "" ;
If (xmlnodes [I]. haschildnodes & xmlnodes [I]. childnodes. Count = 1 & xmlnodes [I]. childnodes [0]. nodetype = Xmlnodetype. Text)
{
Svalue = Xmlnodes [I]. childnodes [0]. value;
}
Else
{
For (Int J = 0; J <xmlnodes [I]. Attributes. Count; j ++)
{
Svalue + = Xmlnodes [I]. attributes [J]. Name +" = "+ Xmlnodes [I]. attributes [J]. Value +" ;";
}
}
If (svalue = "")
{
Tnode. Text = Xmlnodes [I]. Name;
}
Else
{
Tnode. Text = Xmlnodes [I]. Name + ":" + Svalue;
}
Treenodes. Add (tnode );
If (xmlnodes [I]. haschildnodes)
{
Buildtree (xmlnodes [I]. childnodes, tnode. nodes );
}
}
Double-click button to add the followingCode:
private void button#click (Object sender, eventargs e)
{< br> xmldocument Doc = new xmldocument ();
Doc. load (@ "C: \ Documents ents and Settings \ Administrator \ My Documents ents \ Visual Studio 2005 \ projects \ windowsapplication1 \ windowsapplication1 \ xmlfile1.xml");
buildtree (Doc. childnodes, treeview1.nodes); | call recursion
}< br>