How can I pass an array of custom objects through a Web Service?

Here is a brief C# example for passing an array of custom objects through a Web Service. An important point is that the Web service class (CustomObjectArrayWS in the sample code) is decorated with the XmlInclude() attribute, defined in the System.Xml.Serialization namespace.

This is necessary because when you pass an ArrayList into or out of Web services, the SOAP serializer must have access to the metadata (details of methods and properties) for all classes that can be included as elements of the collection. This attribute notifies the serializer to expect elements of the specified type; you can use the attribute many times per attribute target.

<%@WebService Language="c#" class="CustomObjectArrayWS"%>
using System;
using System.Collections;
using System.Web.Services;
using System.Xml.Serialization;
public class CustomObjectArrayWS
{
     	[WebMethodAttribute]
    	[XmlInclude(typeof(Address))]
    	public ArrayList GetAddresses ()
   	{
  		ArrayList al = new ArrayList();
  		Address addr1 = new Address("John Smith", "New York",12345);
  		Address addr2 = new Address("John Stalk", "San Fransisco", 12345);
  			
       		al.Add(addr1);
       		al.Add(addr2);
       		
      		return al;
 	}
} 
// Custom class to be added to the collection to be passed in //and out of the service
public class Address
{
 	public string name;
 	public string city;
 	public int zip; 	
 	// Default ctor needed by XmlSerializer
 	public Address()
 	{
 	}
 	public Address(string _name, string _city, int _zip  )
	{ 
                    this.name = _name;
                    this.city = _city;
                     this.zip = _zip;
           }
       }


Back

 
Printer friendly version of the XML-Webservice-FAQ-Pass-Array-Of-Custom-Objects page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
Key Elements to an Effective Business Continuity Plan
Learn to develop a plan that clarifies what is critical and sets specific recovery requirements.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.