Home   Cover Cover Cover Cover

Complex Parameters, XML Namespaces, Encoding

From Section 7.4.2 of the book

This example demonstrates the following concepts:

  • A web service can have an XML namespace (attribute [WebService(Namespace= ...)]
  • Complex data types (see TimeDesc) can be passed as parameters like primitive data types.
  • The SOAP encoding can be customized (see [SoapElement]). This attribute has no effect if HTTP-GET or HTTP-POST are used. Therefore the automatic test stub that is generated if the web service is opened with Interne Explorer does not show the field TimeZone of the struct TimeDesc as an XML element ZoneID. You should test this web service with Webservice Studio.

<%@ WebService Language="C#" Class="TimeService" %>
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;

[WebService(Namespace="http://dotnet.jku.at/time/", Description="Returns the time")]
public class TimeService : WebService {

  [WebMethod(Description="Returns the time description of the server")]
  public TimeDesc GetTimeDesc() {
    TimeDesc td = new TimeDesc();
    DateTime time = DateTime.Now;
    td.TimeLong = time.ToLongTimeString();
    td.TimeShort = time.ToShortTimeString();
    td.TimeZone = "1";      
    return td;

public struct TimeDesc {
  public string TimeLong;
  public string TimeShort;
  [SoapElement (DataType="nonNegativeInteger", ElementName = "ZoneID")]
  public string TimeZone; 

If you copy this code to a file TimeService.asmx in a virtual directory of your local machine you can open it with Internet Explorer and get a test page that allows you to invoke the method GetTimeDesc.

Alternatively, you can test your web service also with WebService Studio. In this case the result could look as follows:

WebService Studio