Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Displaying XML Data in an ASP.NET page : Binding DataGrid to an XML File
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Displaying XML Data in an ASP.NET page : Binding DataGrid to an XML File

by Sriram Vaideeswaran.

Overview :
The .net framework provides the Dataset object which is designed to handle data abstractly independent of the data source. The DataSet can handle data from variety of sources like SQL, XML etc. In this article we'll show you how to bind a data grid control to data in an XML file using the DataSet class.

In order access the DataSet class you must import the System.Data namespace into your page.

<% @ Import NameSpace = "System.Data" %>

The DataSet has a ReadXML() method which is used to read data from an XML file. For a DataSet to correctly read data from an XML file, the XML file should be of the following format :

<RootElement>
<TableName>
    <FieldName1>Field value</FieldName1>
    <FieldName2>Field value</FieldName2>
    <FieldName3>Field value</FieldName3>
    <FieldName4>Field value</FieldName4>
</TableName>
<TableName>
    <FieldName1>Field value</FieldName1>
    <FieldName2>Field value</FieldName2>
    <FieldName3>Field value</FieldName3>
    <FieldName4>Field value</FieldName4>
</TableName>
......
</RootElement>

Where each TableName section represents a row of data from the table.

Example :

<ProductList>
	<Products>
		<ProductId>1</ProductId>
		<ProductName>Rice</ProductName>
		<Rate>27</Rate>
	</Products>
	<Products>
		<ProductId>2</ProductId>
		<ProductName>Wheat</ProductName>
		<Rate>20</Rate>
	</Products>
</ProductList>

The name of the XML file along with the full path is passed as an argument to the ReadXml method of the DataSet.

DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath("Products1.xml"));

The DataSet can then be bound to the DataGrid using the DataBind() method of the DataGrid.

DG1.DataSource = DS;
DG1.DataBind();

 ( No Further Pages )

Download Associated Files
002E.Zip

Related Articles
  1. Creating an RSS Feed in ASP.NET
  2. Introduction to XML
  3. Reading XML Files with ASP.NET
  4. Introduction to XPath for ASP.NET Developers
  5. Creating new XML Document in ASP.NET Programmatically
  6. Introduction to DTD (Document Type Definition)
  7. Validating an XML Document using DTD in ASP.NET

Comments/Questions ( Threads: 2, Comments: 2 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Problem in reading XML by making a webrequest asyncronously
  2. reading xml tags

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.