New XML Features in .NET Version 2: Part III
XML Data Binding
October 30, 2009
asp:Feature
LANGUAGES:C#
ASP.NETVERSIONS: 2.0
New XML Features in .NET Version 2: Part III
XML Data Binding
By Dan Wahlin
Version 2 of the .NET Framework provides several newenhancements that make it easier than ever for developers to parse, validate,transform, and bind XML data. PartI of this series demonstrated how to parse XML using new methods found inthe XmlReader class and how XML data can be generated using the XmlWriter class.PartII focused on editing XML loaded into memory and validating it using theXmlDocument and XPathNavigator classes. PartII also showed how to perform more efficient XSLT transformations using thenew XslCompiledTransform class.
In this final article in the series you ll be introducedto several new controls available in ASP.NET 2.0 that simplify working withXML. You ll see how to use the XmlDataSource data source control to bind datato different ASP.NET server controls and be introduced to the TreeView control.Before moving ahead to the new XML features found in version 2, it s worthwhileto take a quick look at how XML data binding occurred in .NET 1.1.
XML Data Binding in .NET 1.1
The simplest way to bind XML to server controls in .NET1.1 was to load an XML document into an XmlDataDocument class and then convertit to a DataSet or simply load a document directly into a DataSet using theReadXml method. An example of using ReadXml to bind RSS data to a DataListcontrol named dlRss is shown in Figure 1.
DataSet ds = new DataSet();
ds.ReadXml("http://msdn.microsoft.com/rss.xml");
//RSS items will be placed into the 3rd DataTable
dlRss.DataSource = ds.Tables[2].DefaultView;
dlRss.DataBind();
Figure 1: LoadingXML data into a DataSet using the ReadXml method.
If you re new to RSS, Figure 2 shows a simple example ofan RSS 2.0 document. Notice that details about an article are marked up usingan element. RSS items can have several different child elements thatdescribe an item, such as title, link, pubDate, and creator.
http://www.xmlforasp.net/XML/RssGenerator.aspx/
RSSChannel description.
en-us
Mon, 23 Jan 2006 14:04:35 GMT
Mon, 23 Jan 2006 14:04:35 GMT
XMLfor ASP.NET Generator
1440
asp.netPROMagazine Article
DanWahlin
Sat, 21 Jan 2006 18:00:00 GMT
Figure 2: RSSdocuments allow different types of items to be marked up using elements. Although RSS documents are often used to mark up articles, they canbe used for many other types of things, including product information, softwarepatches, and more.
Once an RSS document such as the one shown in Figure 2 isloaded into a DataSet, the appropriate columns in the DataTable must be boundto the target server control. An example of creating a DataList controlItemTemplate that binds to link and title columns is shown here:
<%#DataBinder.Eval(Container.DataItem,"title") %>
Although this approach doesn t require a lot of code, it doesrequires you to think of the XML data in a relational manner (rows and columns)rather than hierarchically. It also makes locating the data you want from theoriginal RSS document a little tricky because you need to know which DataTablecontains the RSS tags that you want to bind to the DataList. Thisis because of how the DataSet loads XML data. Each level of nesting found in anXML document causes a new DataTable to be created in the DataSet. The tags in an RSS document are automatically loaded into DataTable #3 becausethese tags are nested three levels deep in the XML hierarchy.
If the RSS data being bound to the DataList control needsto be filtered, you ll need to use the DataView class RowFilter propertyrather than filtering with a simple XPath expression. An example of filteringout RSS items that don t have the word XML in the title is shown in Figure 3.
DataSet ds = new DataSet();
ds.ReadXml("http://msdn.microsoft.com/rss.xml");
DataView view = ds.Tables[2].DefaultView;
view.RowFilter = "title LIKE '%XML%'";
DataList1.DataSource = view;
DataList1.DataBind();
Figure 3: FilteringXML data using the DataView class RowFilter property.
This type of code certainly works and is fairly simpleto write but it doesn t allow you to leverage XML technologies to their fullextent and requires more code than should really be necessary for such a simpletask. Let s look at how to eliminate this code completely using new classesavailable in version 2 of the .NET Framework.
Using the XmlDataSource Control
ASP.NET version 2 contains several data source controlsthat are designed to interact with different types of data sources withoutwriting VB.NET or C# code. Data source controls included in .NET version 2include SqlDataSource, ObjectDataSource, AccessDataSource, XmlDataSource, andSiteMapDataSource. The XmlDataSource control derives from a new class namedHierarchicalDataSourceControl and can be used to bind XML documents todifferent ASP.NET server controls, such as the DataList, TreeView, and Menucontrols. It can also be used to bind data to controls normally used withrelational data, such as the GridView and DataList controls.
Adding an XmlDataSource control to an ASP.NET Web Form isdone by using the tag (see Figure 4). In addition toadding the standard ID and Runat attributes, the control also allows you todefine the XML document source using the DataFile property. You can also definewhat nodes contain the actual data that you want to bind by using the XPathproperty. The XPath property value shown in Figure 4 instructs theXmlDataSource control to start at the root element, move down tothe child element, and then select all elementsunder (refer to Figure 2 to see how these elements are nested).
DataSourceID="RSSDataSource"> ...DataList templates
DataFile="http://msdn.microsoft.com/rss.xml" XPath="rss/channel/item" /> Figure 4: Bindingan XmlDataSource data source control to a DataList control. Binding data contained within the XmlDataSource controlcan be done by using the DataList s new DataSourceID property, as shown inFigure 4. At run time, the DataList will locate the XmlDataSource control basedon its ID, and automatically bind the data. You may be wondering how the DataList knows what data tooutput as it binds to the XmlDataSource control. The XmlDataSource s XPathproperty identifies which nodes to bind ( nodes in this example),but RSS nodes can have several different children such as
Read more about:
MicrosoftAbout the Author
You May Also Like