Last week I showed you how to read an XML file, load it to a DataSet and assign those values into a GridView. Today I'll show you how you can read an XML file using the XmlDataSource object.
Let's assume that we have an XML file called Symbols.xml in our App_Data folder
that contains the following data
<?xml version="1.0" encoding="utf-8" ?> <Symbols> <Symbol ExecutionSymbol="ATT" Name="AT&T"></Symbol> <Symbol ExecutionSymbol="MSFT" Name="Microsoft"></Symbol> <Symbol ExecutionSymbol="GOOG" Name="Google"></Symbol> <Symbol ExecutionSymbol="CSCO" Name="Cisco"></Symbol> <Symbol ExecutionSymbol="IP" Name="International Paper Co."></Symbol> <Symbol ExecutionSymbol="MF" Name="MF Global"></Symbol> <Symbol ExecutionSymbol="Q" Name="Qwest Communications International Inc."></Symbol> <Symbol ExecutionSymbol="BMC" Name="BMC Software Inc."></Symbol> <Symbol ExecutionSymbol="WCI" Name="WCI Communities Inc."></Symbol> <Symbol ExecutionSymbol="SPY" Name="SDRs"></Symbol> <Symbol ExecutionSymbol="LEH" Name="Lehman Brothers Holdings Inc."></Symbol> <Symbol ExecutionSymbol="XLF" Name="Financial Select Sector SPDR"></Symbol> <Symbol ExecutionSymbol="QQQQ" Name="PowerShares QQQ TR 1"></Symbol> <Symbol ExecutionSymbol="IWM" Name="IShare Rus 2000 INDX"></Symbol> <Symbol ExecutionSymbol="GE" Name="General Electric Co."></Symbol> <Symbol ExecutionSymbol="MER" Name="Merrill Lynch Co., Inc."></Symbol> <Symbol ExecutionSymbol="BAC" Name="Bank of America Corporation"></Symbol> <Symbol ExecutionSymbol="INTC" Name="Intel Corp"></Symbol> <Symbol ExecutionSymbol="F" Name="Ford Motor Co."></Symbol> <Symbol ExecutionSymbol="QID" Name="UltraShort QQQ ProShares"></Symbol> </Symbols>
and we want to load it to a GridView with no server-side code and a quick and easy way. The answer is to use the XmlDataSource object. The XmlDataSource control is an ASP.NET control that allows you to automatically read XML Data and make that data readily available to any ASP.NET control.
To start using this control, go to your Toolbox and drag the XmlDataSource control to your page.
Once the control is on the page it would popup a dialog that has configuration options for our XmlDataSource control. Click the "Configure Data Source" button to configure our XmlDataSource
A popup like below will come up that allows you to select the Xml file you want to your XmlDataSource object to read. It also gives you the option to select the XSL file. You can also specify an XPath expression to use to filter the data in our Xml.
Click the "Browse" button for the "Data File" option to select an XML file.
A new dialog will appear that will let you navigate the folder tree to select your desired XML file
Click "Ok" and you'll be taken back to the "Configure Data Source" screen. Click "Ok" again to finalize the XML data assignment.
Now that we have the file set in to our XmlDataSource control we need assign it to a control. We can do that by dragging a GridView control to our form.
Next, we need to assign the XmlDataSource control as the data source for our GridView. We can do this by selecting our XmlDataSource from the "Choose Data Source" dropdownlist.
Click "XmlDataSource1" and you will notice that our GridView was automatically updated and now shows the contents of our XML file.
Easy huh? Next up, Consuming Web Services in ASP.NET
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.