Binding Treeview to XML documents

Source: Internet
Author: User
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>

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.