How to Bind XML to TreeView

An ASP.NET TreeView control displays a list of hierarchical data that makes it more convenient to view large amounts of data. ASP.NET Visual Studio has a "DataSource" element you use to import an XML file and bind the data to a TreeView control. You can add the resource to the Web form in the designer, so you do not need to write dynamic code.

Instructions

    • 1

      Click the Windows "Start" button and select "All Programs." Click "Microsoft .NET Framework," then click the "Visual Studio" shortcut to open the designer software. Open your Web project.

    • 2

      Double-click the form that contains the TreeView. If you do not already have a TreeView defined, drag and drop a TreeView control from the toolbox to the Web form.

    • 3

      Right-click the form and select "View Code" to see the ASP HTML tags. Add the XML DataSource tag. The following code shows you how to include an XML file in the ASP form:

      <asp:XmlDataSource ID="xmldata" runat="server" XPath="/PageTitle" DataFile="/data/data.xml" />

    • 4

      Bind the XML file from Step 3 to the TreeView. The following code shows you how to bind the data to the TreeView:

      <asp:TreeView ID="tree1" runat="server" DataSourceID=" xmldata">
      </asp:TreeView>

      Notice the "DataSourceID" contains the ID of the XML file tag created in Step 3.

    • 5

      Save the changes and click the "Run" button to run the code in the ASP debugger.

Related Searches:

References

Comments

Related Ads

Featured