Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Reading XMl file from Javascript in C#

Posted by Viral Sarvaiya on March 23, 2010

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 :

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)]
public class MyService : System.Web.Services.WebService
public MyService()

//Uncomment the following line if using designed components

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");

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();

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

<head id="Head1" runat="server">
<title>Read XML File From Javascript with Web Serice in</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";

var child = xmlDoc.documentElement.firstChild;
while (child) {
child = child.nextSibling;

function OnTimeOut(arg) {
alert("timeOut has occured");

function OnError(arg) {
alert("error has occured: " + arg._message);

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:ServiceReference Path="~/Service.asmx" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Call Service" OnClientClick="CallService()" />

Step 6 : Run The Application Enjoy Coding…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: