hi here i demonstrate another application which read the XML file from the javascript,
in my previous post, i demostrate the webservice calling from javascript, this post i continue from that,
web service return the string which is in XML format and javascript read this XML string and give the alert according to the TAG of the XML.
follow the steps as per the my previous post url : http://viralsarvaiya.wordpress.com/2010/03/23/calling-web-service-from-java-script-in-asp-net-c/
we repeat the Steps,
step 1 : file -> new -> web site
step 2 : select ASP.NET Web Service
step 3 : Add new web form name “default.aspx”
Step 4 : following code shows the “service.cs” file which is the web service file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml ;
using System.IO ;
[WebService(Namespace = "http://Localhost...xys/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class MyService : System.Web.Services.WebService
{
public MyService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string strNoOfData)
{
// Create the xml document containe
XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);// Create the root element
XmlElement root = doc.CreateElement("PHI_Data");
doc.AppendChild(root);
int iNoOfData = 0 ;
if ( int.TryParse( strNoOfData, out iNoOfData ) == false )
iNoOfData = 600 ;
DateTime dtTemp = new DateTime(2009, 1, 1, 12, 0, 0);
Random rndTemp = new Random();
float fValue = ((float)rndTemp.Next(1000, 2000)) / 100;
for (int iCounter = 0; iCounter < iNoOfData; iCounter++)
{
XmlElement Data = doc.CreateElement("PHI_Record");
Data.SetAttribute("Record_Number", iCounter.ToString());
XmlElement dataTimeStamp = doc.CreateElement("TimeStamp");
dataTimeStamp.InnerText = dtTemp.ToString("yyyy-MMM-dd hh:mm:ss");
XmlElement dataValue = doc.CreateElement("Value");
dataValue.InnerText = fValue.ToString();
Data.AppendChild(dataTimeStamp);
Data.AppendChild(dataValue);
root.AppendChild(Data);
fValue = ((float)rndTemp.Next(1000, 2000)) / 100;
dtTemp = dtTemp.AddHours(1);
}
string xmlOutput = doc.OuterXml;
return xmlOutput;
}
}
step 5 : following code shows the “default.aspx” file
</pre>
<head id="Head1" runat="server">
<title>Read XML File From Javascript with Web Serice in asp.net</title>
<script type="text/javascript">
function CallService() {
MyService.HelloWorld(document.getElementById('Textbox1').value, OnComplete, OnError, OnTimeOut);
}
function OnComplete(xmlText) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlText);
var child = xmlDoc.documentElement.firstChild;
while (child) {
alert(child.firstChild.text);
alert(child.lastChild.text);
child = child.nextSibling;
}
}
function OnTimeOut(arg) {
alert("timeOut has occured");
}
function OnError(arg) {
alert("error has occured: " + arg._message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service.asmx" />
</Services>
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Call Service" OnClientClick="CallService()" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
Step 6 : Run The Application Enjoy Coding…


