Before you begin with the client-side code, you should have a server code that will respond to all the requests coming from your client-server code. In this case, I've created a web service to handle the requests. Notice the green comments.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Script.Serialization;
- using System.Web.Script.Services;
- using System.Web.Services;
- using System.Xml.Linq;
- namespace DataReaderService
- {
- /// <summary>
- /// Summary description for EmpReader
- /// </summary>
- [WebService]
- //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.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 EmpReader : System.Web.Services.WebService
- {
- [WebMethod]
- [ScriptMethod(ResponseFormat = ResponseFormat.Json) ] //format the returned string value as JSON string
- public string getEmpData(string id)
- {
- //Create a list of 'employee' object
- List<employee> emps = new List<employee>();
- //Load the XML document
- var employee = XElement.Load( Server.MapPath("Employees.xml") );
- //Select the employee with given ID
- var emp = from q in employee.Elements("Employee")
- where q.Element("EmpId").Value == id
- select q;
- //Return a message to the client if no data returned
- if (emp.Count()==0) { return "no data"; }
- //Iterate through emp collection to populate a list of 'employee'
- foreach (var element in emp)
- {
- //Gets Phone elements
- var e = from j in element.Elements("Phone")
- select j;
- object[] phones = e.ToArray(); //Convert phone to array in order to be able to read their values only
- emps.Add(new employee { Name = element.Element("Name").Value, gender = element.Element("Sex").Value, Address = element.Element("Address").Value, HomePhone = ((XElement)phones[0]).Value , WorkPhone=((XElement)phones[1]).Value });
- }
- //return emps;
- return new JavaScriptSerializer().Serialize(emps); //use this to return as JSON object
- }
- }
- public class employee
- {
- public string Name { get; set; }
- public string gender { get; set; }
- public string HomePhone { get; set; }
- public string WorkPhone { get; set; }
- public string Address { get; set; }
- }
- }
You may change how the server handles the requests to add more functionality. Now it's time to write our client-side code, and here are some different ways:
Classic JavaScript
- var xhttp = new XMLHttpRequest();
- xhttp.onreadystatechange = function () {
- if (xhttp.readyState == 4 && xhttp.status == 200) {
- var server_data = xhttp.responseXML;
- //When using xhttp.responseXML, data can be returned in XML format , we get 'string' TAG contents
- var XMLData = server_data.getElementsByTagName("string")[0].childNodes[0].nodeValue;
- // //Check if data found
- if (XMLData != "no data") {
- $("#txtResult").val(XMLData);
- //parse JSON data
- var Result = JSON.parse(XMLData);
- //Draw Header
- $("#table_results").append("<tr><th>Name</th><th>Gender</th><th>Address</th><th>Home Phone</th><th>Work Phone</th></tr>");
- //Display data
- $("#txtResult").val(Result[0].Name);
- $("#table_results").append("<tr><td>" + Result[0].Name + "</td><td>" + Result[0].gender + "</td><td>" + Result[0].Address + "</td><td>" + Result[0].HomePhone + "</td><td>" + Result[0].WorkPhone + "</td></tr>");
- } else { $("#txtResult").val("No Data found"); }
- }
- };
- xhttp.open("POST", "http://localhost:56998/EmpReader.asmx/getEmpData", true);
- xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xhttp.send("id=" + $("#txtEmpId").val() );

Hadshana KamalanathanPosted Jul 22, 2018, 1:19 AM
Good one...