Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Populating the TreeView Control With XMLDataSource

0.00/5 (No votes)
18 Sep 2011 1  
The TreeView is an .NET  DataBound Control which is used to display hierarchical data.  Can be bound with the The Nodes of this control with the help

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

The TreeView is an .NET  DataBound Control which is used to display hierarchical data.  Can be bound with the The Nodes of this control with the help of XML, tabular or Relational Data. In this example we use XMLDataSouce to bind the TreeView.

XMLDataSouce implements the IHierarchicalDataSouce interface. It simply set the DataSourceId property of the TreeView Control to the ID value of the data source control and the TreeView Control automatically binds to the specified data source Control.

The TreeView Control Contains a DataBindings property that is a collection of TreeNodeBinding objects that define the binding between a DataItem and a TreeNode.

Here I write a simple program to show the binding of XMLDataSource and Treeview.

Step1: First we create a XML File.(in my case XMLFile.xml)

File :-> New :-> File :-> XML File

Step2: Write the following code in it.

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

<Customers>

  <Customer CustomerId="1" Name="Mahak">

    <Orders>

      <Order OrderId="1" ShipDate="06-09-2011">

        <OrderItems>

          <OrderItem OrderItemId="1" PartNumber="123" PartDesc="Large" Quantity="5" Price="22.00"></OrderItem>

          <OrderItem OrderItemId="2" PartNumber="1234" PartDesc="Small" Quantity="6" Price="99.00"></OrderItem>

        </OrderItems>

       

      </Order>

      <Order OrderId="2" ShipDate="09-12-2011">

        <OrderItems>

          <OrderItem OrderItemId="3" PartNumber="153" PartDesc="Medium" Quantity="7" Price="232.00"></OrderItem>

          <OrderItem OrderItemId="4" PartNumber="1236" PartDesc="Half" Quantity="8" Price="299.00"></OrderItem>

        </OrderItems>

 

      </Order>

 

    </Orders>

    <Invoices>

      <Invoice InvoiceId="5" Amount="99.23" />

      <Invoice InvoiceId="6" Amount="88.89" />

    </Invoices>

   

  </Customer>

  <Customer CustomerId="2" Name="Neha">

    <Orders>

      <Order OrderId="3" ShipDate="08-09-2011">

        <OrderItems>

          <OrderItem OrderItemId="11" PartNumber="1223" PartDesc="Large" Quantity="52" Price="22.00"></OrderItem>

          <OrderItem OrderItemId="22" PartNumber="12134" PartDesc="Small" Quantity="62" Price="99.00"></OrderItem>

        </OrderItems>

 

      </Order>

      <Order OrderId="4" ShipDate="09-12-2011">

        <OrderItems>

          <OrderItem OrderItemId="33" PartNumber="153" PartDesc="Medium" Quantity="57" Price="232.00"></OrderItem>

          <OrderItem OrderItemId="44" PartNumber="1236" PartDesc="Half" Quantity="89" Price="299.00"></OrderItem>

        </OrderItems>

 

      </Order>

 

    </Orders>

    <Invoices>

      <Invoice InvoiceId="7" Amount="99.23" />

      <Invoice InvoiceId="8" Amount="88.89" />

    </Invoices>

 

  </Customer>

</Customers>

 

Step 3:  An XMLDataSorce and a TreeView Control add in the Web page (Default.aspx)

<body>

    <form id="form1" runat="server">

    <div>

    <asp:XmlDataSource ID="Xmldatasource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>

        <asp:TreeView ID="TreeView1" runat="server" DataSourceID="Xmldatasource1" ShowLines="true">s

        <DataBindings>

        <asp:TreeNodeBinding DataMember="Customer" TextField="Name" ValueField="CustomerId" />

        <asp:TreeNodeBinding DataMember="Order" TextField="ShipDate" ValueField="OrderId" />

        <asp:TreeNodeBinding DataMember="OrderItem" TextField="PartDesc" ValueField="OrderItemId" />

        <asp:TreeNodeBinding DataMember="Invoice" TextField="Amount" ValueField="InvoiceId" />

        </DataBindings>

        </asp:TreeView>

    </div>

    </form>

</body>

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here