advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
The XmlDataSource control in ASP.NET 2.0 takes web development to the next step by providing a seamless way to consume XML based data in an ASP.NET page without having to write a single line of code. Do you think that encapsulating will simply increase the confusion that many developers already have about the ever-increasing flavors of XML, XSLT, XPath and data-binding? Do you plan to use the new control much? Let us know in the ASP.NET discussion forum on DevX.
Partners & Affiliates
advertisement
advertisement
Average Rating: 4/5 | Rate this item | 1 user has rated this item.
 Print Print
 
XML Data-Binding Powers Up in ASP.NET 2.0
In ASP.NET 2.0, the new XmlDataSource control greatly simplifies binding XML sources to controls, letting you display hierarchical XML data easily. It even leverages XSLT, XPath, and caching, eliminating much manual coding.  

advertisement
ML data is generally used to represent semi-structured or hierarchal data. Using XML documents as your data source allows you to receive XML documents from other sources and format the XML data to be compatible with your application. ASP.NET 1.0 let you use XML data sources, but much of that work required manual coding to get the data into a format suitable for display. In contrast, ASP.NET 2.0 provides a new XmlDataSource control that simplifies binding XML data to controls such as the TreeView and GridView—and it works with both hierarchical and tabular data.


Loading the XmlDataSource Control
Typically, you'd use the XmlDataSource control to display hierarchical XML data in read-only scenarios. However, because the control implements the IDataSource interface, it also works with tabular, or list-style, data. You can load the XmlDataSource control from either an XML file, by setting the DataFile property, or from a string containing XML, by setting the control's Data property to the string.

While loading XML data, you can also specify an XML schema. The XmlDataSource control doesn't use the schema, but data-bound controls can access it if required. To load a schema, you set the SchemaFile property value to the appropriate XML schema file. Alternatively, you can also store a schema in string form directly from code using the Schema property.

This article shows examples of how you might use the XmlDataSource control to display data in hierarchical and tabular data formats. To begin with, here's a simple example that displays XML data from an XmlDataSource control in a TreeView control.

Binding the XmlDataSource Control to a TreeView Control
To begin, create the XML file shown below and save it as ProductDetails.xml. I'll use that file as a data source throughout this article. The file contains a fragment of information on the various products in the Products table in the Northwind database.

   <?xml version='1.0'?>
   <!-- This file represents a fragment of Product 
      store inventory database -->
   <Products>
     <Product ID="1" Name="Chai" 
       Price="13">
       <Desc Value="10 boxes x 20 Bags"/>        
     </Product>
     <Product ID="2" Name="Chang" 
       Price="10">      
       <Desc Value="24 -12 oz bottles"/>      
     </Product>
     <Product ID="3" Name="Tofu" 
       Price="23.25">
       <Desc Value="40-100 kg pkgs"/>   
     </Product>
   </Products>

After creating the XML file, create a new ASP.NET page to consume it and display its information in a TreeView control. For example, the following page code shows a binding example using an XmlDataSource control and a TreeView control.

  <%@ Page Language="VB"%>
   <html xmlns="http://www.w3.org/1999/xhtml" >
   <head runat="server">
     <title>XML Data Binding with a TreeView Control
     </title>
   </head>
   <body>
     <form id="form1" runat="server">
       <div>
         <asp:TreeView ID="TreeView1" Runat="server" 
           DataSourceID="XmlDataSource1">
           <DataBindings>                
             <asp:TreeNodeBinding 
                DataMember="Product" 
                ValueField="ID" 
                TextField="Name">
             </asp:TreeNodeBinding>                                
             <asp:TreeNodeBinding 
                DataMember="Desc" 
                ValueField="Value" 
                TextField="Value">
             </asp:TreeNodeBinding>
          </DataBindings>
         </asp:TreeView>
         <asp:XmlDataSource 
             ID="XmlDataSource1" 
             Runat="server" 
             DataFile="~/Data/ProductDetails.xml">
         </asp:XmlDataSource>
       </div>
     </form>
   </body>
   </html>

 
Figure 1. TreeView Binding Example Page: Output from the XmlDataSource control is data-bound to a TreeView Control, which gets the data from the ProductDetails.xml file.
Note that the <asp:TreeView> elements associate properties of individual TreeNode objects (such as DataMember, ValueField, and TextField) to attributes of XML nodes in the hierarchy. When you perform data binding, attributes in the XML elements are promoted to properties of the data item. Also note that the TreeView hierarchy exactly matches the hierarchy of the source XML; if the source XML doesn't follow the same hierarchy, you would not get desirable results. Because of this, you would normally construct the XML specifically for binding to the TreeView.

When you execute the page, it will look similar to Figure 1.

  Next Page: XPath Data Binding Syntax


Page 1: IntroductionPage 3: Performing XSL Transformations with the XmlDataSource Control
Page 2: XPath Data Binding SyntaxPage 4: Displaying Hierarchical Data in a GridView Control
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES