banner



How To Return Xml From Web Service Vb

  • Updated date Nov 21, 2015
  • 1.1m
  • 39

This commodity explains how to return JSON and XML data from a [WebMethod].

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

Footstep ane

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

empty web site

Step two

At present 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 tin 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 likewise a course for a user data type to ascertain the structure.

Class

[WebSerivce] Course

WebSerivce

User-ascertain information type for JSON and XML Stucture:

Define Data type

Step 4

Edit the Employee Grade and declare a belongings that we need to employ in the JSON and XML object.

Employee Class

Code:

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

Footstep 5

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

Step 6

Side by side nosotros need to create a [WebService] form MyServiceClass and add all the namespaces commencement that are required.

MyServiceClass

Step 7

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

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

    GetEmployeeXML

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

    GetEmployeeJSON

Lawmaking:

  1. using  System;
  2. using  Organization.Collections.Generic;
  3. using  Arrangement.Linq;
  4. using  System.Web;
  5. using  System.Web.Services;
  6. using  Organization.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.                 Proper 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.                 Bacon=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 spider web browser.

output in web browser

There are 2 methods, one from the render ofJSON and the 2nd for the render ofXML event.

Click on any part proper name and Invoke to exam the [WebMethod].

  1. Click on GetEmployessJSON

    Click on GetEmployessJSON

  2. Click on GetEmployessXML

    Click on GetEmployessXML

Thank you lot for reading this commodity.

How To Return Xml From Web Service Vb,

Source: https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/

Posted by: cashsyle1983.blogspot.com

0 Response to "How To Return Xml From Web Service Vb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel