Binding The TreeView Control to XML Data

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>


Similar Articles