Return custom objects from Web Services

Source: Internet
Author: User

 

1. Address class code:
   1:   
   2:  /// <summary>
   3:  /// Summary description for Address
   4:  /// </summary>
   5:  public class Address
   6:  {
   7:      public string Street;
   8:      public string City;
   9:      public int ZIP;
  10:      public string Country;
  11:   
  12:      public Address()
  13:      {
  14:          //
  15:          // TODO: Add constructor logic here
  16:          //
  17:      }
  18:      /// <summary>
  19:      /// Initializes a new instance of the Address class.
  20:      /// </summary>
  21:      /// <param name="_street"></param>
  22:      /// <param name="_city"></param>
  23:      /// <param name="_ZIP"></param>
  24:      /// <param name="_country"></param>
  25:      public Address(string _street, string _city, int _ZIP, string _country)
  26:      {
  27:          this.Street = _street;
  28:          this.City = _city;
  29:          this.ZIP = _ZIP;
  30:          this.Country = _country;
  31:      }
  32:  }
2. Web Service Code:
   1:  using System.Collections;
   2:  using System.Web.Services;
   3:  using System.Xml.Serialization;
   4:   
   5:   
   6:  /// <summary>
   7:  /// Summary description for CustomObjService
   8:  /// </summary>
   9:  [WebService(Namespace = "http://tempuri.org/")]
  10:  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11:  // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  12:  // [System.Web.Script.Services.ScriptService]
  13:  public class CustomObjectService : System.Web.Services.WebService {
  14:   
  15:      public CustomObjectService () {
  16:   
  17:          //Uncomment the following line if using designed components 
  18:          //InitializeComponent(); 
  19:      }
  20:   
  21:      [WebMethodAttribute]
  22:      [XmlInclude(typeof(Address))]
  23:      public ArrayList GetArrayList()
  24:      {
  25:          ArrayList list = new ArrayList();
26: Address add1 = new Address (", No. 20", "Beijing", 100093, "China ");
27: Address add2 = new Address ("No. 39 Yuquan Road", "Beijing", 100092, "China ");
28: Address add3 = new Address ("Shenxian Road, 59", "Guiyang", 30095, "China ");
  29:          Address add4 = new Address("Washiton Street, 30", "Washiton", 200056, "USA");
  30:          list.Add(add1);
  31:          list.Add(add2);
  32:          list.Add(add3);
  33:          list.Add(add4);
  34:          return list;
  35:      }
  36:      
  37:  }
  38:   
3. Page code and output results
   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  
   6:  
   7:      <title></title>
   8:  
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:      <asp:GridView ID="grdAddress" runat="server"></asp:GridView>
  13:      
  14:      </div>
  15:      </form>
  16:  </body>
  17:  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.