First Static, Here is the design file and no codebehind
<asp:XmlDataSource ID="dsXML" runat=server DataFile="cd_catalog.xml"
XPath="/CATALOG/CD" ></asp:XmlDataSource>
<asp:TreeView runat="server" ID="xmlTree" DataSourceID="dsXML">
<DataBindings>
<asp:TreeNodeBinding DataMember="TITLE" TextField="#InnerText" />
<asp:TreeNodeBinding DataMember="ARTIST" TextField="#InnerText" />
<asp:TreeNodeBinding DataMember="COUNTRY" TextField="#InnerText" />
</DataBindings>
</asp:TreeView>
and few lines of xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
Now Dynamic,the dynamic one is little bit tricky,
<h3>TreeView Constructor Example</h3>
<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource" DataFile="Book.xml" runat="server">
</asp:XmlDataSource>
<br /><br />
<asp:Label id="Message" runat="server"/>
using System;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
public partial class XmlMgt_XmlReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){
// Create a new TreeView control.
TreeView NewTree = new TreeView();
// Set the properties of the TreeView control.
NewTree.ID = "BookTreeView";
NewTree.DataSourceID ="BookXmlDataSource";
// Create the tree node binding relationship.
// Create the root node binding.
TreeNodeBinding RootBinding = new TreeNodeBinding();
RootBinding.DataMember = "Book";
RootBinding.TextField = "Title";
// Create the parent node binding.
TreeNodeBinding ParentBinding = new TreeNodeBinding();
ParentBinding.DataMember = "Chapter";
ParentBinding.TextField = "Heading";
// Create the leaf node binding.
TreeNodeBinding LeafBinding = new TreeNodeBinding();
LeafBinding.DataMember = "Section";
LeafBinding.TextField = "Heading";
// Add bindings to the DataBindings collection.
NewTree.DataBindings.Add(RootBinding);
NewTree.DataBindings.Add(ParentBinding);
NewTree.DataBindings.Add(LeafBinding);
// Manually register the event handler for the SelectedNodeChanged event.
NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);
// Add the TreeView control to the Controls collection of thePlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree);
}
protected void Node_Change(object sender, EventArgs e){
// Retrieve the TreeView control from the Controls collectionof the PlaceHolder control.
TreeView LocalTree =(TreeView)ControlPlaceHolder.FindControl("BookTreeView");
// Display the selected node.
Message.Text = "You selected: " + LocalTree.SelectedNode.Text;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book Title="Communication Skill">
<Chapter Heading="Chapter 1">
<Section Heading="Section 1">
</Section>
<Section Heading="Section 2">
</Section>
</Chapter>
<Chapter Heading="Chapter 2">
<Section Heading="Section 1">
</Section>
</Chapter>
</Book>
</Books>
This is shorter than earlier with code-behind apporach
.aspx.cs file
protected void Page_Load(Object sender, EventArgs e)
{
TreeView NewTree = new TreeView();
// Set the properties of the TreeView control.
NewTree.ID = "BookTreeView";
NewTree.DataSourceID ="BookXmlDataSource";
// Manually register the event handler for the SelectedNodeChanged event.
NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);
// Add the TreeView control to the Controls collection of the PlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree);
}
protected void Node_Change(Object sender, EventArgs e)
{
// Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");
// Display the selected node.
Message.Text = "You selected: " + LocalTree.SelectedNode.Text;
}
Here is the design file: .aspx file
<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource" DataFile="cd_catalog.xml"
runat="server">
</asp:XmlDataSource>
More shorter of the TreeView, absolutely in codebehind
.aspx.cs file
protected void Page_Load(Object sender, EventArgs e)
{
//creating an instance of XmlDatasource
XmlDataSource ds = new XmlDataSource();
//connecting the source with file
String _path = Server.MapPath("cd_catalog.xml");
ds.DataFile = _path;
//binding the file
ds.DataBind();
//creating an instnace of TreeView
TreeView NewTree = new TreeView();
//Telling its source
NewTree.DataSource = ds;
//binding the TreeView with DataSource
NewTree.DataBind();
//creating a PlaceHolder
PlaceHolder plcHolder = new PlaceHolder();
//adding the PlaceHolder with FORM
form1.Controls.Add(plcHolder);
//adding the TreeView Control to the PlaceHolder
plcHolder.Controls.Add(NewTree);
}