The New Data Binding Syntax for XML Data
Increasingly, the data you have to work with in your Web applications is presented as XML. It might be static XML documents, or (more likely) XML that has been dynamically generated by another application or a Web Service. ASP.NET 2.0 contains several New Data Source Controls, And Three of There Are Specify Designed to Work with XML Data:
The XmlDataSource control is designed to work with hierarchical data presented as XML, in other words data where there can be multiple and irregular nesting of elements. The DataSetDataSource control is designed to work with XML that represents a flat table, rather than being hierarchical in nature . In other words, XML data that effectively represents a DataTable within a DataSet. The SiteMapDataSource control is a logical extension of the XmlDataSource control and is specifically designed to expose an XML-formatted map of a Web site or application. It is generally used along with several other new controls in ASP.NET 2.0 that generate menus and other site navigation aids. This article does not explore the new data source controls, but instead focuses on the way that data binding is used to access and present individual items of data that THESE CONTROLS Expose. The xpathbinder Object The Issue At Hand Is: How Do You Specify An Element OR A Series of Elements When Creating Output Wher e the data is xml rather tour more usual "rows and columns"
? Format The existing Eval and Bind methods rely on the column names and are used when the source data is a rowset -. Such as that exposed by the SqlDataSource control The answer is that there is a new binding object in ASP.NET 2.0, called the XPathBinder. This works much like the DataBinder object that is now the default context when data binding to rowset data. The XPathBinder object exposes two methods that can be used to select items from an XML document, the Eval method and the Select method. The eval method, which has exactly the same syntax as the DataBinder.Eval method, returns the value of a single element or attribute from the current context. The current context is effectively the set of elements exposed by the parent of this element, rather like the . set of columns in the current row of a relational data rowset You can optionally format the returned value using the same approach as described for the DataBinder.Eval method: <% # XPathBinder.Eval (Container.DataItem, "xpath" [, " fly ORMAT "])%>
. However, in line with the simplified syntax for rowset data binding, the equivalent has been provided for XML data sources as well Rather than specifying the XPathBinder.Eval method, you simply use a method of the TemplateControl named XPath:
<% # Xpath ("xpath" [, "format"])%>
The second method of the XPathBinder is the Select method. This method is designed to return a collection of node values, rather than a single value (like the Eval / XPath method does). For this reason, there is no format parameter for the Select Method. The full syntax of this method is:
<% # Xpathbinder.select (container.dataitem, "xpath")%>
Again, There is a simplified form:
<% # XPathSelect ( "xpath")%> The xpath parameter to the XPath and XPathSelect methods is an XPath expression that identifies the nodes that you want to bind to the controls in your page. XPath expressions are extremely powerful, and can be used to return single nodes (elements or attributes) from anywhere in an XML document, or collections of nodes. for more details of XPath and the techniques for writing XPath expressions, start at the W3C site at http://www.w3.org/ Tr / XPath. Using the Xpath Method to Demonstrate The XPath Method, We NEED SOME XML TO ACT As The Source Data. For Simplicity, This Example Uses a Disk file named Employees.xml, Which Looks Like this:
XML Version = "1.0"?>
Employee>
Employee>
... more Employees Here ...
Employee-List>
TO Expose this for data binding, we use an xmldatasource control, specifying the disk file as the xml source data:
/> Now the data can be bound to any control using the new XML data binding statements. This example uses a FormView control, with the DataSourceID attribute set to the ID of the XmlDataSource control so as to link the two controls together. Then, inside the ItemTemplate section, individual controls that generate the output for the FormView control are declared, just as in the earlier examples that used a SqlDataSource control. This time, however, the XPath data binding method is used, because the items in the source data are XML nodes : ID: Text = '<% # xpath ("@ ID")%>' /> b> /> Name: Text = '<% # XPath ("Name")%>' /> b> /> DEPT: Text = '<% # xpath ("divartment")%>' /> b> Phone: Text = '<% # xpath ("phone")%>' /> b> Itemtemplate> asp: formview> Each Label control in the ItemTemplate of the FormView control is bound to one of the nodes in the source data. The nature of the XML file means that the phone-list> employee> phone-list> Employee> ... more Employees Here ... Employee-List> An XmlDataSource control is used to expose this data for data binding, in exactly the same way as the previous example. However, the XPathSelect method can now be used to get a collection of phone numbers for each employee. In the next listing, the first Section of the itemtemplate is the same as in the prepvious example. ID: Text = '<% # xpath ("@ ID")%>' /> b> /> Name: Text = '<% # XPath ("Name")%>' /> b> /> DEPT: Text = '<% # xpath ("divartment")%>' /> b> Phone number: Itemtemplate> ask: repeater> Itemtemplate> ask: formview> The difference is in the addition of a Repeater control to display the list of phone numbers for each employee. The XPathSelect method is used to set the DataSource for the Repeater to a collection of the elements containing the phone numbers. The XPath "phone-list / phone "selects all the .. Format The aim is to explain and demonstrate the uses of the new data binding syntax, rather than how the data source and display controls themselves work But, as you have seen, the combination of all three technologies - data binding, data source controls and the new list and grid controls - makes it easy to create attractive pages for displaying data with very little effort in summary, there are three types of data binding statement you can use in ASP.NET 2.0 for rowset data (ie data that.. IS in row and column format) You can Use the existing evan Syntax, or the simplified evan, to bind to a data column. and, as in v1.x, you can also useful Another Control, or to the results of an expression: <% # Container.DataItem ("[Column-name | Property | Field]")%> <% # DataBinder.eval ("[Column-name | Property | Field]" [, "Format"])%> <% # Eval ("[Column-name | Property | Field]" [, "Format"])%> When using the new GridView, DetailsView or FormView controls, where you implement automatic updates to the data source and use templates to display the data, you must use the new Bind method for any controls containing values that are used as parameters in the SQL INSERT, Update and delete statements: <% # Bind ("Column-name" [, "Format"])%> When the source data is XML, rather than a rowset, you must use the XPath or XPathSelect method in the data binding statements. Both of these methods accept an XPath expression that identifies the node or nodes to select. The XPath method returns a value from A Single Node (Attribute or Element), While the Xpathselect Method Returns A Collection (as an arraylist) of values from multiple matching nodes: <% # xpath ("xpath-expression" [, "format"])%> <% # Xpathselect ("xpath-expression")%> With all this choice in data binding capabilities, You'll Never Again Have to Write Code That Itreates Through Your Data To Build Up An Html Table! About the Author Alex Homer began his love-hate relationship with computers in 1980 with the Altair and Sinclair Z80 Since 1994 he has been working with and writing about various programming technologies -.. From databases to building Help systems However with the growth of the Web, and particularly as ASP rapidly become a viable application platform, he concentrated almost entirely on this topic. Alex has written or contributed to more than 30 books on Web development topics for the major publishers, as well as producing articles for ASPToday, DevX and other sites. He is an MVP and a member of the INETA speaker's bureau, and speaks regularly on a range of Web development topics at conferences such as Microsoft PDC, Tech-Ed, ASP Connections, WebDevCon and VS.NET Live. In what spare time is left, HE Runs His OWN Software and Consultancy Company Stonebroom Limited.