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>
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.