SIGN UP MEMBER LOGIN:    
ARTICLE

Binding The TreeView Control to XML Data

Posted by Manish Dwivedi Articles | XML in C# January 15, 2008
This article explain how to bind the TreeView control to an XML file.
Reader Level:

To bind the TreeView control to XML data, I have taken the XmlDataSource in which I have define an Xpath and a DataFile.

Here is my sample code:

<asp:XmlDataSource ID="PageDataSource" runat="server" XPath="/PageTitle" DataFile="~/App_Data/PageTitle.xml" />
<asp:TreeView ID="PageTreeView" runat="server" DataSourceID=" PageDataSource ">
  <DataBindings>
    <asp:TreeNodeBinding DataMember="Id" TextField="#InnerText"  />
    <asp:TreeNodeBinding DataMember="Tite" TextField="#InnerText" />
    <asp:TreeNodeBinding DataMember="PageUrl" TextField="#InnerText" />
  </DataBindings>
</asp:TreeView>

In the page load simply write:

protected void Page_Load(object sender, EventArgs e)
{
    PageDataSource.XPath = "/PageTitle";
    PageTreeView.DataBind();
}

Lastly, here is the XML file:

<?xml version="1.0" encoding="UTF-8"?>

<PageTitle>

  <Page>

    <Id>1</Id>

    <Tite>This is my testing</Tite>

    <PageUrl>mybook /1/1.aspx</PageUrl>

  </Page>

  <Page>

    <Id>2</Id>

    <Tite>This is my testing</Tite>

    <PageUrl> mybook /2/2.aspx</PageUrl>

  </Page>
</PageTitle>

Login to add your contents and source code to this article
share this article :
post comment
 

I have 2 xml files one file name is Admin.xml and second file name is User.xml. As on logon if Admin gets login then i want to bind Admin.xml file to treeview and at Users User.xml to bind in treeview.. it would be possible in treeview at runtime to bind any xml file. pl rply urgently. thank you

Posted by sharad patil Mar 01, 2011

http://bhushanbharat83.blogspot.com/2010/11/binding-treeview-dynamically-in-aspnet.html

Posted by Bharat Bhushan Nov 27, 2010

Hi how i hide Id  to display in treeview

Posted by adil ahmed Oct 19, 2009

Hi,
 Use SqlDataSource instead of XmlDataSource.

Posted by Manish Dwivedi Feb 11, 2009

how we can bind data with treeview within sqlserver in asp.net

Posted by aaaaa aaaaa Feb 11, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor