ASP Unit4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Unit-4 Using XML

Writing Datasets to XML :-


- .NET provides a name space which is used to work with XML file called
System.XML namespace.
- It has following number of classes :
1) XML Document: It is used to create XML file or document.
2) XML DataDocument: It is used to create database XML document.
3) XML Node: It is used to XML node element for XML file.
4) XML Comment: It is used to provide the comment in XML document.
5) XML Element: It is used to create XML element in XML file.
6) XML Attribute: It is used to create attribute for XML element.
7) XML NodeList: It is used to create and store list of node collection
elements in XML file.
Writing Dataset to XML :-
- You can write data from dataset or database in XML file you see two
methods write XML:
1) WriteXML():-
- It is used to read data from dataset, we must follow following rules:
a) All tables of datasets are treated as main element of XML file.
b) All rows of tables are treated as sub elements of XML file.
c) All columns of tables are treated as data elements which contain the actual
data of table.
d) Also there is only one root element as XML rule.
2) WriteXMLSchema():-
- It is a small bit of XML schema definition XSD file of XML basic part.
- XML schema is a file which contains rules for defining XML element and
their data.
- If we use only writeXML() the only data would be written from data set to
XML file but with WriteXMLSchema() the schema information would be
write with XML file.
Reading Dataset with XML :-
- You can read XML file and store the information in form of data set or data
table using two methods.
1) ReadXML():
- It is used to read XML data from ML file to a data set or data table. When
we read data table it follow following rules:
a) All main elements under root elements are treated as different table.
b) It would automatically store data in XML way.
2) ReadXMLSchema():
- It is used to read XML Schema definition file from XML files, XML schema
s a file which has rules for defining XML element and their data.
- If we read data using readXML() then it would just read file the entire data
from the XML file and store the data into dataset or data table but before
reading you need to specify some rules then it can be mention by using
readXMLSchema() mehod.
Web Services :-
- Web Services are one of the new features that are introduce by Microsoft. It
is a web based functionality provided by ASP.NET.
- Web services can be defined as group of web methods where each web
method provides some specific functionality. All the web method would be
combining in a single web service and this web service will be uploaded on
web server so that anyone can use them.
- Web services provide a number of web methods which provides different
functionality. It can be used by only web as well as window application.
- The main feature of web service is that it can be used by different
application regardless of programming languages, operating system and
hardware platforms which provide or use. That is web services are platform
independent.
- Web services are group of web methods the application which provides web
services can be called web services client.
- At a time only one method would be used from the group of method
available.
Remote method call using XML :-
- The basic thing that works behind any web service is XML and HTTP.
HTTP is used for sharing information over the internet.
- XML is standard language which s used for sharing information among
different platform as it is platform independent.
- Web methods that we create same like normal methods which have
arguments and which also returns value. This arguments and return value are
given in form of XML language which is platform independent.
- As the data is set via XML language thus it makes the data also platform
independent.
- XML provides standard data representation format which can be understand
by any platform.
SOAP (Simple Object Access Protocol) :-
- XML serves as the base for any web services. Web service parameters and
returns value which are written in XML.
- This value needed to be passed through internet for this SOAP is used. It is
also platform independent.
- SOAP is XML based protocol which allows different application to share
information over HTTP. Using SOAP the XML data will be transfer from
one location to another. SOAP is standard communication protocol that is
used for interchanging environment in structure format.
- Whenever a client application make a request for web method SOAP packet
will be created. This packet contains the name of web method to be access
and invoked. It also contains the parameters passed to web method in an
XML format.
- When the SOAP packet come a web server side then the web method name
and its parameters are extracted from the packet. Then the method is
invoked, executed and the return value if available is sent back to client
using SOAP packet.
Web Service Description Language (WSDL) :-
- After defining the web services, the web services should be define in
standard format. So that it can be access by any one. The developer who is
using web service must know the parameters, return values and body of the
web service. This information should be available in standard format so that
it can be shared among different platform.
- This is achieving by using XML based description language called WSDL.
- WSDL is a language that is used to describe web service and its web
methods. It is a markup language which describe the web services and it’s
web methods.
- WSDL contains following information related to web services.
1) Information related to all web services which are specify or created under
particular website.
2) Information related to the purpose of web services.
3) Information related to types of parameters and return values under web
service.
4) Information related to format in which a web service method can be
accessed.
5) Information related to URL to which web services can be accessed.
Building and consuming a web service :-
- Take the following steps to create the web service:

Step (1) : Select File -> New -> Web Site in Visual Studio, and then select
ASP.NET Web Service.
Step (2) : A web service file called Service.asmx and its code behind file,
Service.cs is created in the App_Code directory of the project.
Step (3) : Change the names of the files to StockService.asmx and
StockService.cs.
Step (4) : The .asmx file has simply a WebService directive on it:
<%@ WebService Language="C#"
CodeBehind="~/App_Code/StockService.cs" Class="StockService" %>
Step (5) : Open the StockService.cs file, the code generated in it is the basic
Hello World service. The default web service code behind file looks like the
following:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml.Linq;

namespace StockService
{
// <summary>
// Summary description for Service1
// <summary>

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]

// To allow this Web Service to be called from script,


// using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]

public class Service1 : System.Web.Services.WebService


{
[WebMethod]

public string HelloWorld()


{
return "Hello World";
}
}
}
Step (6) : Change the code behind file to add the two dimensional array of
strings for stock symbol, name and price and two web methods for getting
the stock information.
using System;
using System.Linq;

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml.Linq;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script,


// using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]

public class StockService : System.Web.Services.WebService


{
public StockService () {
//Uncomment the following if using designed components
//InitializeComponent();
}

string[,] stocks =
{
{"RELIND", "Reliance Industries", "1060.15"},
{"ICICI", "ICICI Bank", "911.55"},
{"JSW", "JSW Steel", "1201.25"},
{"WIPRO", "Wipro Limited", "1194.65"},
{"SATYAM", "Satyam Computers", "91.10"}
};

[WebMethod]
public string HelloWorld() {
return "Hello World";
}

[WebMethod]
public double GetPrice(string symbol)
{
//it takes the symbol as parameter and returns price
for (int i = 0; i < stocks.GetLength(0); i++)
{
if (String.Compare(symbol, stocks[i, 0], true) == 0)
return Convert.ToDouble(stocks[i, 2]);
}

return 0;
}

[WebMethod]
public string GetName(string symbol)
{
// It takes the symbol as parameter and
// returns name of the stock
for (int i = 0; i < stocks.GetLength(0); i++)
{
if (String.Compare(symbol, stocks[i, 0], true) == 0)
return stocks[i, 1];
}

return "Stock Not Found";


}
}
Step (7) : Running the web service application gives a web service test page,
which allows testing the service methods.
Step (8) : Click on a method name, and check whether it runs properly.
Step (9) : For testing the GetName method, provide one of the stock
symbols, which are hard coded, it returns the name of the stock
Consuming the Web Service

- For using the web service, create a web site under the same solution. This
could be done by right clicking on the Solution name in the Solution
Explorer. The web page calling the web service should have a label control
to display the returned results and two button controls one for post back and
another for calling the service.
- The content file for the web application is as follows:

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


CodeBehind="Default.aspx.cs" Inherits="wsclient._Default" %>

<!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>
Untitled Page
</title>
</head>

<body>

<form id="form1" runat="server">


<div>
<h3>Using the Stock Service</h3>

<br /> <br />

<asp:Label ID="lblmessage" runat="server"></asp:Label>

<br /> <br />

<asp:Button ID="btnpostback" runat="server" onclick="Button1_Click"


Text="Post Back" style="width:132px" />

<asp:Button ID="btnservice" runat="server"


onclick="btnservice_Click" Text="Get Stock" style="width:99px" />

</div>
</form>

</body>
</html>
The code behind file for the web application is as follows:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

//this is the proxy


using localhost;

namespace wsclient
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblmessage.Text = "First Loading Time: " +
DateTime.Now.ToLongTimeString
}
else
{
lblmessage.Text = "PostBack at: " +
DateTime.Now.ToLongTimeString();
}
}

protected void btnservice_Click(object sender, EventArgs e)


{
StockService proxy = new StockService();
lblmessage.Text = String.Format("Current SATYAM Price:{0}",
proxy.GetPrice("SATYAM").ToString());
}
}
}

You might also like