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 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) Then DS.ReadXml(C:\Company.xml) Cache(MyDataSet) = DS End If Stage2 : Attach DataSet to control via the DataView Object : the DataView object allows us sort the results and : improve the final result DV = DS.Tables("Company").DefaultView DV.Sort = "CompanyName ASC" selecting which column in the DataView to populate the dropdown with CompanyPick.DataTextField = "CompanyName" CompanyPick.DataSource = DV Stage3 : Render the control, setting the first item as the default : selected item as a final improvement. CompanyPick.DataBind() CompanyPick.SelectedIndex = 0 The company.xml file is as follows: <Companys> <Company> <CompanyName>Leading Light Consultancy</CompanyName> <CompanyID>001</CompanyID> <Telephone>123456</Telephone> <PostCode>Pcode1</PostCode> </Company> <Company> <CompanyName>Solander PLC</CompanyName> <CompanyID>002</CompanyID> <Telephone>222222</Telephone> <PostCode>Pcode2</PostCode> </Company> <Company> <CompanyName>Rhys corp</CompanyName> <CompanyID>003</CompanyID> <Telephone>333333</Telephone> <PostCode>Pcode3</PostCode> </Company> <Company> <CompanyName>Thomas Ltd</CompanyName> <CompanyID>004</CompanyID> <Telephone>4444444</Telephone> <PostCode>Pcode4</PostCode> </Company> <Company> <CompanyName>Company A</CompanyName> <CompanyID>005</CompanyID> <Telephone>555555</Telephone> <PostCode>Pcode5</PostCode> </Company> <Company> <CompanyName>A Company Ltd</CompanyName> <CompanyID>006</CompanyID> <Telephone>6666666</Telephone> <PostCode>Pcode6</PostCode> </Company> </Companys> Additional 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.  

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish