ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > Working with XML
 

ASP.NET Tutorial - Working with XML

                                                                                                                 Page 2 of 3
 

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.

       


 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.