Bind XML data to the TreeView Control :
A TreeView control is used when we want to display some data in a hierarchical
format. In this post we are going to see the tree view control in ASP.net and
how can we use it to display data from an XML file.
Let us create a new web site in Visual studio. On the design form of the ASPX
page, drag and drop a TreeView control. We are going to bind XML data to the
treeView. So we will have the XMLDataSource added to the form.
The design from will have XMLDataSource and TreeView control added
We will add some formatting to the TreeView control to make it more presentable.
Click on the button as shown
Click on “Auto Format” and select a scheme.
Now we specify the path of our XML file in the DataFile property of the
XMLDataSource.
<asp:XmlDataSource
ID="XmlDataSource1"
runat="server"
DataFile="XML_FILE_PATH"
XPath="/categories"></asp:XmlDataSource>
On the page load of the ASPX page we write the code to bind the XML data source
to the TreeView
protected
void Page_Load(object
sender, EventArgs e)
{
TreeView1.DataSource = XmlDataSource1;
TreeView1.DataBind();
}
The treeview is displayed on
the ASPX page. The XML file which contains the data is attached here with the
article.