Web Service in ASP.NET (Return JSON and XML): Part 4

First we need to create a Web service as was already discussed in Part 1. Again create a new Web Service in Visual Studio.

Step 1

Open Visual Studio and select "File" -> "New" -> "Web Site...".

empty web site

Step 2

Now add a Web Service file (.asmx) to the Web site.

Web site

Provide a name for the web service file.

service file

And delete the existing code file from the Solution Explorer so that we can create our own class file to host on this web service.

host on this web service

Step 3

Now add a new class file to [WebService] and also a class for a user data type to define the structure.

Class

[WebSerivce] Class

WebSerivce

User-define data type for JSON and XML Stucture:

Define Data type

Step 4

Edit the Employee Class and declare a property that we need to use in the JSON and XML object.

Employee Class

Code:

  1. public class Employee  
  2. {  
  3.     public int Id { getset; }  
  4.     public string Name { getset; }  
  5.     public int Salary { getset; }  
  6. }  

Step 5

Now edit your .asmx file to define the CodeBehind and Class Properties.

Step 6

Next we need to create a [WebService] class MyServiceClass and add all the namespaces first that are required.

MyServiceClass

Step 7

Write the [WebService] attribute over the class name and create the [WebMethod].

 

  1. GetEmployeeXML() for returning the data in XML.

    GetEmployeeXML

  2. GetEmployeeJSON() for returning the data in JSON.

    GetEmployeeJSON

Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. using System.Web.Script.Services;  
  7. using System.Web.Script.Serialization;  
  8. [WebService]  
  9. public class MyServiceClass  
  10. {  
  11.     [WebMethod]  
  12.     public Employee[] GetEmployessXML()  
  13.     {  
  14.         Employee[] emps= new Employee[] {  
  15.             new Employee()  
  16.             {  
  17.                 Id=101,  
  18.                 Name="Nitin",  
  19.                 Salary=10000  
  20.             },  
  21.             new Employee()  
  22.             {  
  23.                 Id=102,  
  24.                 Name="Dinesh",  
  25.                 Salary=100000  
  26.             }  
  27.         };  
  28.         return emps;  
  29.     }  
  30.     [WebMethod]  
  31.     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
  32.     public string GetEmployessJSON()  
  33.     {  
  34.         Employee[] emps = new Employee[] {  
  35.             new Employee()  
  36.             {  
  37.                 Id=101,  
  38.                 Name="Nitin",  
  39.                 Salary=10000  
  40.             },  
  41.             new Employee()  
  42.             {  
  43.                 Id=102,  
  44.                 Name="Dinesh",  
  45.                 Salary=100000  
  46.             }  
  47.         };  
  48.         return  new JavaScriptSerializer().Serialize(emps);  
  49.     }  
  50. }  

Step 8

Build the application and view the output in a web browser.

output in web browser

There are two methods, one from the return of JSON and the second for the return of XML result.

Click on any function name and Invoke to test the [WebMethod].

 

  1. Click on GetEmployessJSON

    Click on GetEmployessJSON

  2. Click on GetEmployessXML

    Click on GetEmployessXML

Thank you for reading this article.


Similar Articles