Using XML as data source for server controls

XML has long been adopted as common format between various computer systems, as a result you are often confronted with a piece of XML that you need to display on the screen. The DataGrid is great t

DevPro Staff

November 14, 2003

2 Min Read
ITPro Today logo

XML has long been adopted as common format between various computer systems, as a result you are often confronted with a piece of XML that you need to display on the screen. The DataGrid is great tool for doing this however I shall use the dropdown list control to demonstrate the concept (I have actual used this many times on our clients projects).
Stage 1: The first step is to extract the data from its XML form into a DataSet. The DataSet has numerous creation interfaces. In this example I shall use the readXML method. The DateSet could also be obtained from the cache (see stage2).
Stage 2: Secondly you have a DataSet which needs to be attached to the control. The DataSet can simply be attached to the server control using the DataSource method. However often you would like to modify the DataSet in someway to improve the quality of the final result (can also be done at stage 3). To increase performance this is a good place to cache the DataSet, the DataSet can be cached and accessed as an object directly in ASP.NET
Finally render the server control(s) them selves. This could be as simple as running the DataBind method or perhaps you use this stage to make final adjustments to the result (can also be done at stage 2).
Dim DS as New DataSet(), DV as New DataView()
Stage1 : extract data from XML checking cache first.DS = CType(Cache(MyDataSet), DataSet)If IsNothing(DS) ThenDS.ReadXml(C:Company.xml)Cache(MyDataSet) = DSEnd IfStage2 : Attach DataSet to control via the DataView Object : the DataView object allows us sort the results and : improve the final resultDV = DS.Tables("Company").DefaultViewDV.Sort = "CompanyName ASC"selecting which column in the DataView to populate the dropdown withCompanyPick.DataTextField = "CompanyName"CompanyPick.DataSource = DVStage3 : Render the control, setting the first item as the default : selected item as a final improvement.CompanyPick.DataBind()CompanyPick.SelectedIndex = 0The company.xml file is as follows:Leading Light Consultancy001123456Pcode1Solander PLC002222222Pcode2Rhys corp003333333Pcode3Thomas Ltd0044444444Pcode4Company A005555555Pcode5A Company Ltd0066666666Pcode6Additional Tip: It is rare that the XML file has the exact structure you want, therefore it is often necessary to use an XSL transformation first to manipulate the XML into a suitable format. Additional Tip2: All the examples you will find XSL files as static files which is fine for getting the syntax correct. However if you need some dynamic logic in your XSL file this file can be made up dynamically at run time and loaded into the XSL object and then applied to the XML file. 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like