Web service and XML data source
In this blog , I am going to show you how to use XML data source using a Web Service. We consume this Web Service from a normal ASPX page, using jQuery script. I reused the code and here is the link.
From the Web Service, we can get all the employees, all female or male employees. In the Server-side code, we use XML file as the data source and we handle the data received from the XML file as XML nodes use LINQ.
Here is the web service code:
Note: Do not forget to add the Yellow-highlighted line to allow consuming the Web Service, using the client-side code.
Here, we consume the Web Service form a normal ASPX page, but use jQuery mechanism. In this code, given below, we bind both the grids with tables to enable adding the data from the Server. If a grid is not bound, it cannot be displayed.
aspx code
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Xml.Linq;
-
- namespace LINQ.XML
- {
- public partial class XML_TASKS : System.Web.UI.Page
- {
- XElement xelement;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
-
- BindColumnToGridview();
- }
- }
-
- private void BindColumnToGridview()
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("EmpId");
- dt.Columns.Add("EmpName");
- dt.Rows.Add();
- GridAllEmps.DataSource = dt;
- GridAllEmps.DataBind();
- GridAllEmps.Rows[0].Visible = false;
-
-
- DataTable dt1 = new DataTable();
- dt1.Columns.Add("Employee ID");
- dt1.Columns.Add("Employee Name");
- dt1.Rows.Add();
- GridEmployees.DataSource = dt1;
- GridEmployees.DataBind();
- GridEmployees.Rows[0].Visible = false;
-
-
- }
-
- }
- }
In client-side code
HelloWorld method is the only method for testing the data exchange between the Server and the Browser. We use
$.ajax method to send and receive the Server data in JSON format.
Consumer - client-side code:
- $(document).ready(function () {
-
-
-
-
-
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "http://localhost:4000/EmployeeServices.asmx/HelloWorld",
- data: "{}",
- dataType: "json",
- success: function (data) {
-
- $("#txtMsg").val(data.d);
-
- },
- error: function (result) {
- $("#txtMsg").val("Error!");
- }
- });
-
-
-
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "http://localhost:4000/EmployeeServices.asmx/getFemaleEmployees",
- data: '{}',
- dataType: "json",
- success: function (data) {
-
-
- for (var i = 0; i < data.d.length; i++) {
- $("#GridEmployees").append("<tr><td>" + data.d[i].EmpId + "</td><td>" + data.d[i].EmpName + "</td></tr>");
- }
- },
- error: function (result) {
- alert("Error reading the web method getMaleEmployees");
- }
- });
-
-
-
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "http://localhost:4000/EmployeeServices.asmx/getAllEmployees",
- data: "{}",
- dataType: "json",
- success: function (data) {
-
- $("#txtEmps").val(data.d[0].EmpId + " " + data.d[0].EmpName);
-
-
- for (var i = 0; i < data.d.length; i++)
- {
- $("#GridAllEmps").append("<tr><td>" + data.d[i].EmpId + "</td><td>" + data.d[i].EmpName + "</td></tr>");
- }
- },
- error: function (result) {
- alert("Error reading the web method getAllEmployees");
- }
- });
-
-
- $("#btn_men").click(function (e)
- {
-
- $("#GridAllEmps").find("tr:gt(0)").remove();
-
-
- e.preventDefault();
-
-
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "http://localhost:4000/EmployeeServices.asmx/getMaleEmployees",
- data: '{gender:"Male"}' ,
- dataType: "json",
- success: function (data) {
-
- $("#txtEmps").val(data.d[0].EmpId + " " + data.d[0].EmpName);
-
-
- for (var i = 0; i < data.d.length; i++) {
- $("#GridAllEmps").append("<tr><td>" + data.d[i].EmpId + "</td><td>" + data.d[i].EmpName + "</td></tr>");
- }
- },
- error: function (result) {
- alert("Error reading the web method getMaleEmployees");
- }
- });
- });
-
- });
Notice: Change the Web Service address, marked by the highlighted lines.
Web.config note
Change the highlighted number from 6 to 5, if you get ISO error.
- <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:5 /nowarn:1659;1699;1701">
Due to a size limit, I uploaded the project files in the link, given below:
Decryption key is given below:
- !ju1LnmV4AYq7QFSL-UGViUIwyvqZhEfRPN1xb81RxXk