CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, October 03, 2006

Retrieving XML Data with XmlDataSource Control - Accessing and Updating Data in ASP.NET 2.0

Introduction
With its new declarative data source controls and improved data Web controls, ASP.NET 2.0 has greatly simplified the process of working with data. In Part 2 of this article series, Accessing Database Data, we looked at how to use the SqlDataSource control to retrieve data from a relational database, which could then be bound to any data Web control (such as the GridView, DetailsView, FormView, DataList, Repeater, and so on). XML data can be accessed (and displayed) just as easily with the XmlDataSource control.

The XmlDataSource control makes accessing, filtering, and transforming XML data a simple, code-free process. Additionally, the XPath() and XPathSelect() databinding methods added to ASP.NET 2.0 make displaying particular XML values or binding entire XML nodesets just as easy. And the XML data accessed can be from a local file or automatically downloaded from a specified URL. In this article we will examine how to use the XmlDataSource control and the XPath() and XPathSelect() databinding methods, displaying the results in a variety of data Web controls. Read on to learn more!

XmlDataSource Basics
The XmlDataSource control exists merely as a proxy for retrieving XML data, which can then be programmatically accessed or bound to a data Web control. To access XML data from an ASP.NET page using the XmlDataSource control, start by dragging the control from the Toolbox onto the Designer (the XmlDataSource control is found in the Data section of the Toolbox). From the XmlDataSource's smart tag, click the "Configure Data Source" link to bring up the Configure Data Source wizard (shown below). From the wizard, you can set the XmlDataSource control's three most useful properties:

  • DataFile - specifies the file that contains the XML data to access. This can be a local file, like ~/App_Data/XMLFile.xml, or a remote file, like http://msdn.microsoft.com/rss.xml.
  • TransformFile - XML documents can be transformed from one structure to another using a markup language known as XSL Transformations, or XSLT. If you want to transform the XML contents in DataFile before working with the XML data, specify the XSLT file to perform the transformation here. For more information on XSLT, check out the XSLT Tutorial at W3 Schools.
  • XPath - XPath is an XML syntax used to filter the contents of an XML document, returning a particular value or nodeset. If you want to work with a particular set of XML data, specify an XPath expression here to limit the results. See W3 School's XPath tutorial for more information.

The XmlDataSource control wizard. 

Accessing and Updating Data in ASP.NET 2.0: Retrieving XML Data with XmlDataSource Control

Post a Comment