XML Binding

XML is a fundamental part of the .NET architecture. If you look at how data is handled in the Data namespace, you will see that it all revolves around XML. So if you are not familiar with XML basics, pick up some reading material on XML, such as Steve Holzner’s Inside XML (New Riders Publishing, 2001).

Figure 10.4 shows an example of a problem that you might run into. However, the system does not throw an exception or give you any indication that there is a problem.

Figure 10.4. aspx output.


Everything looks fine on the page, but it seems to be missing a few records. Take a look at the example code in Listings 10.14 and 10.15 and XML file in Listing 10.16 and see if you can identify the problem.

Listing 10.14. Using XML as a Data Source (C#)
private void Page_Load(object sender, System.EventArgs e) 
             {
                   // Put user code to initialize the page here 
                   DataSet ds = new DataSet(); 
                   ds.ReadXml("c:\portfolio.xml"); 
                   DataGrid1.DataSource = ds; 
                   DataGrid1.DataBind(); 
             } 

Listing 10.15. Using XML as a Data Source (Visual Basic .NET)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load 
         ' Put user code to initialize the page here 

         Dim ds As New DataSet() 
         ds.ReadXml("c:\portfolio.xml") 
         DataGrid1.DataSource = ds 
         DataGrid1.DataBind() 

    End Sub 

Listing 10.16. portfolio.xml File
<PRODUCT> 
<SYMBOL>IBM</SYMBOL><COMPANY>Int'l Business Machines</COMPANY><PRICE> 
$357,840 </PRICE> 
</PRODUCT> 

<PRODUCT> 
<SYMBOL>WEINX</SYMBOL> <COMPANY> AIM Weingarten Fund A</COMPANY><PRICE>> 
$318,150</PRICE> 
</PRODUCT> 

<PRODUCT> 
<SYMBOL>CSTGX</SYMBOL>              <COMPANY> AIM Constellation Fund A</COM
PANY><PRICE> $256,603</PRICE> 
</PRODUCT> 

<PRODUCT> 
<SYMBOL>KGGAX</SYMBOL>              <COMPANY> Kemper Aggressive Growth A </COM
PANY><PRICE>$121,600</PRICE> 
</PRODUCT> 

<PRODUCT> 
<SYMBOL>SONE</SYMBOL>               <COMPANY>S1 Corporation 
</COMPANY><PRICE>$111,960</PRICE> 
</PRODUCT> 

As you can see, it is very simple to load an XML file into a dataset and assign it to the data grid’s data source property. Microsoft has done a nice job of implementing XML and data-bound controls together.

At first sight, this may look well formed, but there is one small problem. You are missing the root element! One way to make sure that your file is well formed is to look at it with IE 5.x. If it can’t parse the file, it will tell you what the problem is. Figure 10.5 shows what happens when we tried to look at the file with IE.

Figure 10.5. IE XML view.


If you simply add an element called PRODUCTS at the beginning and end of the file encompassing all the XML text, you will create a root element, as shown in Listing 10.17.

Listing 10.17. XML File Corrected
<PRODUCTS> 
<PRODUCT> 
<SYMBOL>IBM</SYMBOL><COMPANY>Int'l Business Machines</COMPANY><PRICE> 
$357,840 </PRICE> 
</PRODUCT> 

<PRODUCT> 
<SYMBOL>WEINX</SYMBOL><COMPANY> AIM Weingarten Fund A</COMPANY><PRICE> 
$318,150</PRICE> 
</PRODUCT> 
</PRODUCTS> 

Now that you have everything set up correctly, you will get a list of records displayed on your web page the way you intended to. Keep in mind that this XML file could be formatted in a number of ways to store just about any type of data you need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset