For reading the data from XML file , we first create the xml file name Sample.xml the We will create an ASP.NET page which reads the contents of this XML file and then displays it to the user.
Creation of XML File Name Sample.xml
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 admin="true">Rakesh Kumar</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:
Default2.aspx
Creation of XML File Name Sample.xml
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 admin="true">Rakesh Kumar</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"));
In this example we have created a page Default2.aspx which have literal control through that we will show the content of xml file. The code of aspx and cs file are given below.
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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>
Default2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, 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;
}
}
Result will be
Name | Rakesh Kumar |
Title | Sample XML Document |
Body | The body of the article goes here. |
Now that we have created the ASP.NET which successfully reads and displays the contents of our XML file
No comments:
Post a Comment