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...".
Step two
At present add a Web Service file (.asmx) to the Web site.
Provide a name for the web 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.
Step 3
Now add a new class file to [WebService] and likewise a course for a user data type to ascertain the structure.
[WebSerivce] Course
User-ascertain information type for JSON and XML Stucture:
Step 4
Edit the Employee Grade and declare a belongings that we need to employ in the JSON and XML object.
Code:
- public class Employee
- {
- public int Id { get ; set ; }
- public string Name { get ; set ; }
- public int Salary { go ; set ; }
- }
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.
Step 7
Write the [WebService] attribute over the grade name and create the [WebMethod].
- GetEmployeeXML() for returning the data in XML.
- GetEmployeeJSON() for returning the information in JSON.
Lawmaking:
- using System;
- using Organization.Collections.Generic;
- using Arrangement.Linq;
- using System.Web;
- using System.Web.Services;
- using Organization.Web.Script.Services;
- using System.Web.Script.Serialization;
- [WebService]
- public class MyServiceClass
- {
- [WebMethod]
- public Employee[] GetEmployessXML()
- {
- Employee[] emps=new Employee[] {
- new Employee()
- {
- Id=101,
- Proper name="Nitin" ,
- Salary=10000
- },
- new Employee()
- {
- Id=102,
- Name="Dinesh" ,
- Salary=100000
- }
- };
- return emps;
- }
- [WebMethod]
- [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
- public string GetEmployessJSON()
- {
- Employee[] emps =new Employee[] {
- new Employee()
- {
- Id=101,
- Name="Nitin" ,
- Bacon=10000
- },
- new Employee()
- {
- Id=102,
- Name="Dinesh" ,
- Salary=100000
- }
- };
- return new JavaScriptSerializer().Serialize(emps);
- }
- }
Step 8
Build the application and view the output in a spider 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].
- Click on GetEmployessJSON
- 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