Binding to XML Files

only for RuBoard

Although an in-depth analysis of XML and its place in the .NET Framework is beyond the scope of this chapter, you do need to know how to use an XML file as a data source for server controls, and the DataGrid in particular. Chapter 12, "XML and SOAP," will explore what XML is and its role in the .NET Framework. This section will only show you how to open and read an existing XML file and bind the data to a DataGrid .

An XML file is nothing more than a text file that's been marked up according to the XML standard, much like an HTML file is simply marked -up text. Since you cannot connect to a text file the way you connect to a database, you don't use the ADO.NET Managed Providers to access the XML file. Instead, you must open and read the XML file using functionality from the System.IO namespace. The class libraries in System.IO include the FileStream and StreamReader classes. You'll use these classes to open and read an XML file. As with any namespace that isn't included by default, you must import System.IO using the @ Import directive.

The FileStream class is used to read from or write to files in the Web server's file system. FileStream can open a file either synchronously (by default) or asynchronously.

Asynchronously opening a file ( BeginRead , BeginWrite ) will begin accessing the file and move on to the next operation without waiting for the function to complete. This method of opening files can cause uncertain completion order when multiple files are opened at once.

When files are opened synchronously ( Read , Write ), each function starts, and completes before the next function begins.

To read an XML file, you use the FileMode.Open and FileAccess.Read parameters of the FileStream class.

 myFileStream = new FileStream(  [fileName],   [fileMode],   [fileAccess]  ) 

The StreamReader class implements a TextReader and reads bytes from a stream. In this case, the FileStream class creates the stream. StreamReader takes FileStream as a parameter and defaults to UTF-8 encoding, which handles Unicode characters correctly and makes the StreamReader appropriate for localized versions of the operating system:

 myStreamReader = new StreamReader(  [fileStream]  ) 

The DataSet class exposes a ReadXml() method, which is used to read XML data from the StreamReader and fill a DataTable with it. The DataTable is created dynamically when ReadXml() is called. Listing 5.8 shows an ASP.NET Web Form that uses FileStream , StreamReader , and DataSet.ReadXml() to open an XML file and stream it into a DataTable in the DataSet . Once it's in the DataSet , you can bind the DataTable of XML data to a DataGrid .

Listing 5.8 Data Binding to XML Data
 [VB] 01: <%@ Page Language="VB" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.IO" %> 04: 05: <script runat="server"> 06:   Sub Page_Load(Source As Object, E As EventArgs) 07:     Dim myDataSet As New DataSet 08: 09:     Dim myFileStream As FileStream = New FileStream(Server.MapPath("states.xml"), graphics/ccc.gif FileMode.Open, FileAccess.Read) 10:     Dim myXmlStream As StreamReader = New StreamReader(myFileStream) 11:     myDataSet.ReadXml(myXmlStream) 12:     myFileStream.Close 13: 14:     myDataGrid.DataSource = myDataSet.Tables("State") 15:     myDataGrid.DataBind() 16:   End Sub 17: </script> [C#] 01: <%@ Page Language="C#" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.IO" %> 04: 05: <script runat="server"> 06:   void Page_Load(Object sender, EventArgs e){ 07:     DataSet myDataSet = new DataSet(); 08: 09:     FileStream myFileStream = new FileStream(Server.MapPath("states.xml"), graphics/ccc.gif FileMode.Open, FileAccess.Read); 10:     StreamReader myXmlStream = new StreamReader(myFileStream); 11:     myDataSet.ReadXml(myXmlStream); 12:     myFileStream.Close(); 13: 14:     myDataGrid.DataSource = myDataSet.Tables["State"]; 15:     myDataGrid.DataBind(); 16:   } 17: </script> [VB & C#] 18: <html> 19: <head> 20: <title>Programming Datadriven Web Applications with ASP.NET - Chapter 5</title> 21: </head> 22: <body> 23:   <asp:DataGrid id="myDataGrid" runat="server" /> 24: </body> 25: </html> 

Note

The states.xml file can be downloaded from the companion Web site for this book at <http://www.samspublishing.com>. Copy the states.xml file to the same directory as the ASP.NET Web Form in Listing 5.8.


In Listing 5.8, you stream in data from an XML file named states.xml . On line 3, you import the System.IO namespace, which enables the Input/Output functionality that's required to read from a file in the file system. On line 9, you create a new FileStream to open and read the states.xml file. The path to the file was created dynamically using the Server.MapPath() method that has been retained from previous versions of ASP. On line 10, you create a new StreamReader named myXmlStream and pass in FileStream as its path. On line 11, you use the DataSet.ReadXml() method to read the XML data from StreamReader .

At this point, you've created a DataTable in the DataSet and filled it with the XML file's data. The DataSet.DataSetName property is set to the XML file's schema ID ( States ), and the DataTable.TableName property is set to the element name ( State ). The XML schema for the states.xml file is shown in Listing 5.9.

Listing 5.9 The states.xml Schema
 01: <?xml version="1.0"?> 02: <root> 03: <xsd:schema id="States" targetNamespace="" 04:   xmlns="" xmlns:xsd=http://www.w3.org/2001/XMLSchema 05:   xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 06:   <xsd:element name="State"> 07:     <xsd:complexType> 08:       <xsd:all> 09:         <xsd:element name="Name" minOccurs="0" type="xsd:string" /> 10:         <xsd:element name="Abbreviation" minOccurs="0" type="xsd:string" /> 11:       </xsd:all> 12:     </xsd:complexType> 13:   </xsd:element> 14: </xsd:schema> 

On line 14 of Listing 5.8, you set the DataSource property of the DataGrid to the State DataTable in the DataSet . When the page is requested , the XML data is bound to the DataGrid . The resulting Web page is shown in Figure 5.7.

Figure 5.7. XML data can be streamed into a DataSet . Then a server control, such as the DataGrid , can bind to the DataTable .
graphics/05fig07.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net