Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

ASP.NET 2.0 Quickstart Tutorials


The XmlDataSource control belongs to the family of data source controls in ASP.NET, which enables a declarative databinding model against a variety of underlying data stores. The XMLDataSource control supports declarative databinding to XML files. XmlDataSource is an example of a hierarchical data source, which supports the required interfaces to allow hierarchical data-bound controls, such as TreeView and Menu, to traverse the hierarchy of nodes to produce their rendering. XmlDataSource also supports the tabular data source interfaces required to bind list controls like GridView, DropDownList, and DataList.

XmlDataSource supports a DataFile property for specifying the path to an XML data file to be used as input. You may also specify a TranformFile property to apply an XSLT transformation to the data and an XPath property for specifying a subset of nodes to expose from the data source.

The example below demonstrates a TreeView control bound to an XML file through an XmlDataSource control. The TreeView associates properties of individual TreeNode objects to attributes of XML nodes in the hierarchy (attributes are promoted to properties of the data item for the sake of data binding). By default, the TreeView control simply renders the data item by calling ToString() on the object. This renders the element name of the XML node so that you can see the hierarchy of the nodes the TreeView is bound against. This does not necessarily produce the desired rendering, but it gives you a starting point for further customizing the way the XML data will be displayed.

VB Binding TreeView to an XML File
Run Sample View Source

To give the TreeView a more meaningful rendering, you can specify individual data bindings for nodes in the tree. TreeNodeBinding objects may be added to the TreeView's Databindings collection for the purpose of defining how the fields of hierachical data items are mapped to TreeNode properties. There are two key properties of TreeNodeBinding that determine the set of hierarchical data items to which the binding applies. The DataMember property specifies the type of data item, or in the case of XML data, the element name for which a binding applies. The Depth property specifies the depth in the hierarchy at which the data binding should apply. You can set either DataMember or Depth, or you can set both of these properties. For example, to define data bindings for all Book elements in a given XML file, set DataMember to "Book". To define bindings for all nodes at depth 1, set the Depth property to 1. To define bindings for all Book nodes at depth 1, set both DataMember to "Book" and Depth to 1 on the TreeNodeBinding object.

Once you have set DataMember or Depth to match a given set of nodes, you can define additional properties of TreeNodeDataBinding to define how properties of the data item (or XML node attributes, in the case of XML data) map to the properties of the TreeNodes that the TreeView control renders. For example, the TextField property defines the name of property/attribute to use for the Text of the TreeNode. Similarly, the ValueField property defines the data item property/attribute to use for the TreeNode Value. The NavigateUrlField property defines the field/attribute to use for TreeNode's NavigateUrl, and so on. You can also specify static values for the TreeNode properties for a given data binding. For example, to specify that TreeNodes for Book elements have a "Book.gif" image, set the ImageUrl property on the TreeNodeBinding whose DataMember property is set to "Book".

The following example shows a TreeView bound to the same XML data as the preceding example, with DataBindings defined for specific elements in the XML hierarchy.

VB TreeView Data Bindings
Run Sample View Source

The XmlDataSource supports an XPath property you can use to filter the set of nodes exposed by the data source. In the example below, the XPath property is set to Bookstore/genre[@name='Business']/book, to filter the nodes of the data source to show only those book elements under the "Business" genre. Be careful to specify correct syntax for the XPath property; otherwise, the data source may expose no nodes (and the asssociated data-bound control will not render).

VB TreeView to an XPath Result
Run Sample View Source

Note that the TreeView hierarchy exactly matches the hierarchy of the source XML. Because of this, it is either common to construct XML specifically for binding to the TreeView or to use an XSL transformation to "re-shape" the data into an appropriate hierarchy for binding to the TreeView.

VB TreeView to an XSLT Transformation
Run Sample View Source

It is also possible to bind a tabular data-bound control to XmlDataSource, however the control only renders the first level of hierarchy in this case. In the example below, a templated DataList control is bound to the same bookstore XML file from the preceding example. Because the top-level nodes exposed from the data source are <book/> nodes, the DataList can bind to properties of those nodes in its ItemTemplate using Eval data binding expressions.

VB DataList Bound to XML File
Run Sample View Source

Although rendering one level of hierarchy is useful, it would be better if we could nest tabular data-bound controls to reflect the underlying hierarchy. Fortunately, ASP.NET 2.0 allows you to do just that. In addition to the Eval data binding syntax, ASP.NET 2.0 provides an XPath-based data binding syntax that is supported on any data item that implements the IXPathNavigable interface. There are two supported expression types:
  • XPath(expression, [formatString]) - Evaluates an XPath expression against the data item, returning a single value.
  • XPathSelect(expression, [formatString]) - Evaluates an XPath expression against the data item, returning a selected list of nodes.
The example below builds upon the preceding example, using the XPath data binding expressions instead of Eval expressions to bind to attributes of the book nodes. At face value, this appears to do nothing more than introduce an '@' prefix to each expression, which is the XPath syntax to refer to a node attribute. However, the real flexibility of XPath lies in its ability to refer to arbitrary items within the hierarchy (not just attributes).

The example adds another DataList to the ItemTemplate of the outer DataList, and binds the DataSource property of this inner DataList to an XPathSelect expression representing the list of chapter nodes for the current book node. In the ItemTemplate of the inner DataList, XPath data binding expressions are evaluated against these 'chapter' context nodes. Using this technique, ASP.NET 2.0 allows you to easily construct rich, hierarchical rendering of data by composing tabular data-bound controls.

VB Nested DataList Bound to XML File
Run Sample View Source