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 : Reading XML Files with ASP.NET
 
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

Reading XML Files with ASP.NET

by Faisal Khan.

Introduction
In this tutorial, we will learn how to read the contents of an XML file with ASP.NET. We will make use of the sample XML file which we created in the previous tutorial (Introduction to XML). We will create an ASP.NET page which reads the contents of this XML file and then displays it to the user.

This tutorial is 2nd one in the series of tutorials about XML and ASP.NET:

Sample XML File
Okay, let us get started now. Copy and paste the contents below in a new text file and then save that file as "sample.xml" in the /App_Data sub-folder of your ASP.NET web application. Placing the XML file in /App_Data sub-folder ensures that no one will be able to directly access this file from the web. It will only be accessed by our ASP.NET page which will read and display its contents.

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>The body of the article goes here.</body>
</article>

Reading the Contents of XML File using XmlDocument class
System.Xml namespace has a class with the name of XmlDocument which we will use to read XML file's contents and display it to the user. Reading the contents of sample.xml file is as easy as these two lines:

	XmlDocument doc = new XmlDocument();
	doc.Load(Server.MapPath("~/App_Data/sample.xml"));

Before we delve ourselves deeper into the code, we should create the ASP.NET page first and study its code later.

Reader.aspx
Copy and paste following code into an ASP.NET page and then save it as "reader.aspx":

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.Xml" %>

<script runat="server">
protected void Page_Load(object source, EventArgs e)
{
	XmlDocument doc = new XmlDocument();
	doc.Load(Server.MapPath("~/App_Data/sample.xml"));

	XmlNode root = doc.DocumentElement;
	AuthorLiteral.Text = root.SelectSingleNode("author").ChildNodes[0].Value;
	TitleLiteral.Text = root.SelectSingleNode("title").ChildNodes[0].Value;
	BodyLiteral.Text = root.SelectSingleNode("body").ChildNodes[0].Value;
}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Reading an XML File</title>
    <style type="text/css">
		body { font-family: Verdana; font-size: 9pt; }
		.name { background-color: #F7F7F7; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<table width="50%" cellpadding="5" cellspacing="2">
		<tr>
			<td class="name">Name</td>
			<td><asp:Literal ID="AuthorLiteral" runat="server" /> 
			<asp:Literal ID="AdminLiteral" runat="server" /></td>
		</tr>
		<tr>
			<td class="name">Title</td>
			<td><asp:Literal ID="TitleLiteral" runat="server" /></td>
		</tr>
		<tr>
			<td class="name">Body</td>
			<td><asp:Literal ID="BodyLiteral" runat="server" /></td>
		</tr>
		</table>
		
    </div>
    </form>
</body>
</html>

When you have properly placed sample.xml file in /App_Data sub-folder and reader.aspx in your web application; you should run the ASP.NET page by accessing it in your browser. On my computer, the ASP.NET page, when run, looked like this:

ASP.NET page displaying the contents of an XML File
ASP.NET page displaying the contents of an XML File

Now that we have created the ASP.NET which successfully reads and displays the contents of our XML file, we should look at the code which is doing the job.


 ( 1 Remaining ) Next

Comments/Questions

No Comments Found.


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.