The output is shown below: -
In the subroutine of the above example, the instance of the DataSet class is created. Next, ReadXml method is called to read Xml file (Items.xml). Further, the DataSet is bound to the Data Grid control, and the menu is displayed.
Using Schema with ReadXml
In certain applications, you want the data types of the elements to be correctly represented. To do so, you need to supply an XML schema with the Xml file. The following file contains both schema and XML data in the same file.
| Example58:SchemaFile.xml |
<Items>
<xsd:schema id="Menu"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="FoodItems">
<xsd:complexType>
<xsd:all>
<xsd:element name="Food" minOccurs="0" type="xsd:string"/>
<xsd:element name="Price" minOccurs="0" type="xsd:decimal"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name="Menu" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element ref="FoodItems"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<FoodItems>
<Food>
Toast
</Food>
<Price>
14.45
</Price>
</FoodItems>
<FoodItems>
<Food>
Noodles
</Food>
<Price>
30.00
</Price>
</FoodItems>
</Items> |
| |
| Example59: ReadSchemaFile.aspx |
<%@ Import Namespace="System.Data" %>
<Script Runat="Server">
Sub Page_Load
Dim dstMenu As DataSet
dstMenu = New DataSet ()
dstMenu.ReadXml( MapPath( "SchemaFile.xml" ) )
rptItems.DataSource = dstMenu
rptItems.DataBind ()
End Sub
</Script>
<html>
<head><title>ReadSchemaFile.aspx</title></head>
<body>
<asp: Repeater
ID="rptItems"
Runat="Server" >
<ItemTemplate>
<hr>
<p><b>Items:</b>
<br><%# Container.DataItem ( "Food" )%>
<p><b>Food Data Type:</b>
<br><%# Container.DataItem( "Food" ).GetType %>
<p><b>Price:</b>
<br><%# Container.DataItem( "Price" )%>
<p><b>Price Data Type:</b>
<br><%# Container.DataItem( "Price" ).GetType %>
</ItemTemplate>
</asp:Repeater>
</body>
</html> |
| |
The output is shown below: -
In the subroutine above, the ReadXml () method retrieves the SchemaFile.xml file into DataSet. The DataSet is then bound to a Repeater control, which displays the values of all the elements by using Repeaters.
Writing an XML Document from a DataSet
The DataSet classes include several methods for retrieving an XML representation of the data contained in a DataSet. If a string representation of XML data is to be retrieved the GetXml () method can be used as shown in the following example:-
| Example60: GetMyXml.aspx |
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim conNorthwind As SqlConnection
Dim dadEmployees As SqlDataAdapter
Dim dstEmployees As DataSet
Dim strXmlData As String
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
dadEmployees = New SqlDataAdapter( "Select * From Employees", conNorthwind )
dstEmployees = New DataSet()
dadEmployees.Fill ( dstEmployees, "Employees" )
strXmlData = dstEmployees.GetXml()
Response.Write( "<pre>" & Server.HtmlEncode( strXmlData ) & "</pre>" )
End Sub
</Script> |
| |
The output is shown below: -
In the above example, the DataSet is dealing with Employees table of Northwind database of SqlServer. After employees table is added to the DataSet, the GetXml () method retrieves a string that contains an XML representation of the DataSet. This string is assigned to a variable named strXmlData and output is obtained using the Response object.
|