Feb. 25, 2011, 11:02 a.m.
posted by pitbull
XML Data Source Controls and Data Binding
ASP.NET developers, and Web developers in general, have used XML in many ways to generate output in a Web page and have been well supplied with tools and components to help with this task. For example, in ASP.NET 1.x, you can use the Xml server control to display an XML document and can optionally apply an XSL or XSLT transformation to it first.
However, ASP.NET is changing the approach to connecting to data sources with the advent of a series of data source controls. The three that apply to XML are listed below.
We'll look briefly at these three controls in the next subsections. For more details on data source controls and their use in data binding with ASP.NET, refer to the companion book to this one, A First Look at ASP.NET v. 2.0 (Boston, MA: Addison-Wesley, 2004, ISBN 0-321-22896-0).
The XmlDataSource Control
To give you some idea of what the data source controls do, the following example uses an XmlDataSource control to load XML data and expose it for data binding. The XML is a list of "extra" packages for cars and has the format shown in Listing 7.20 (abbreviated to save space).
<Automobiles> <Manufacturer Make="Audi"> <Car Model="A4" Id="02347"> <Package Trim="Sport"/> <Package Trim="Luxury"/> <Package Trim="Cold Weather"/> </Car> <Car Model="A6" Id="02932"> ... packages ... </Car> <Car Model="A8" Id="09381"> ... packages ... </Car> </Manufacturer> <Manufacturer Make="BMW"> ... cars and packages ... </Manufacturer> <Manufacturer Make="Porsche"> ... cars and packages ... </Manufacturer> </Automobiles>
A TreeView control is designed to display hierarchical data like this XML document and can be used to display it in an ASP.NET page without requiring any code to be written. An XmlDataSource control specifies the path to the XML document. The TreeView control is bound to the XmlDataSource control by specifying its id property value as the DataSourceID property of the TreeView control (see Listing 7.21).
<form runat="server"> <asp:XmlDataSource id="ds1" runat="server" DataFile="XmlDataSource.xml" /> <asp:TreeView id="tv1" DataSourceID="ds1" runat="server"> <Bindings> <asp:TreeNodeBinding DataMember="Manufacturer" TextField="Make" /> <asp:TreeNodeBinding DataMember="Car" TextField="Model" /> <asp:TreeNodeBinding DataMember="Package" TextField="Trim" /> </Bindings> </asp:TreeView> </form>
The XML document contains nested repeated elements, so the bindings must be declared or all that will be displayed are the element names. Within the TreeView element declaration are three bindings that specify that the control should bind to the Manufacturer element and expose the value of the Make attribute as the text of the nodes, then bind to the Car elements and expose the Model attribute of each one as the text of the nodes, and finally bind to the Package elements and display the value of the Trim attribute as the node values. The result can be seen in Figure.
Rather than specifying the XML as a file path and name for the DataFile property, the XML can be declared in-line in a <Data> element within the <asp:XmlDataSource> element. And you can also specify the schema and stylesheet as attributes in the declaration of the control or assign the values at runtime.
As well as all these features, the control supports caching to improve performance and can be used to update the XML disk file if required. Also note that an XPath can be specified to filter or select which of the elements in the XML source will be exposed. The outline declaration of the XmlDataSource control is shown in Listing 7.22.
<asp:XmlDataSource id="String" runat="server" DataFile="String" ReadOnly="[True|False]" SchemaFile="String" TransformFile="String" AutoSave="[True|False]" XPath="String" UniqueID="String" EnableCaching="[True|False]" CacheDuration="Integer" CacheExpirationPolicy="[Absolute|Sliding]" OnDataSourceChanged="EventHandler" OnTransforming="EventHandler" > <Data> [...In-line XML Data...] </Data> <Schema> [...In-line XML Schema...] </Schema> <Transform> [...In-line XSL/XSLT Transform...] </Transform> </asp:XmlDataSource>
The DataSetDataSource Control
The DataSetDataSource control is similar to the XmlDataSource control but works only with nonhierarchical XML data, that is, data that contains only one "level" of elements within the root element. The root element is exposed as the "table" name, and the nested elements as the columns of this "table," as in Listing 7.23.
<Root> <Column>value</Column> <Column>value</Column> <Column>value</Column> ... </Root>
The DataSetDataSource control does not provide the XPath and UniqueID attributes. However, it has an extra method named GetDataSet that exposes the XML source data as a standard ADO.NET DataSet, allowing for runtime manipulation of the content in code.
The WebServiceDataSource Control
The WebServiceDataSource control can expose hierarchical data and is designed to interact with a Web Service and expose the XML content it returns. Three properties specific to this control specify how it connects to the Web Service: URL (a required String value), TypeName (an optional String value), and Timeout (an optional Integer value).
For more details, see the companion book mentioned earlier.