I've been playing these last few days with ASP.NET WebServices [1], and, naturally, I've been running into a few snags. Yesterday I ran into what seems to be an inconsistency in the way XML serialization works where WebServices are invoked. Consider this simple ASMX file:

<%@ WebService Language="C#" class="WeirdBhv" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;
[ XmlRoot(Namespace="/schemas/customer") ]
public class Customer
{
[ XmlElement("name") ]
public string Name;
[ XmlElement("phone") ]
public string Phone;
}
[ WebService(Namespace="/services/WeirdBhv") ]
public class WeirdBhv
{
[ WebMethod ]
public Customer GetCustomer()
{
Customer c = new Customer();
c.Name = "Tomas";
c.Phone = "12223222";
return c;
}
}

Now, here's the weirdness. If you invoked this webservice via the ASP.NET SOAP binding, the response would look something like this:


xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">



string

string




Notice how the Customer instance is serialized on an element named GetCustomerResult. Now, if you were to invoke the service with the HTTP POST binding, you'd get this, instead:



string

string

Notice that this time the Customer instance is serialized with a Customer element name. My question is why this difference? I know that if I were to specify the element name in the XmlRoot attribute in the declaration of Customer, in both cases it would be serialized with that name. However, I find it very inconsistent that it doesn't use the same element name in both cases even if the XmlRoot didn't specify the element name to use.

[1] Yes, I've actually worked more with other toolkits, such as the SOAP Toolkit than with ASP.NET.... go figure.


Tomas Restrepo

Software developer located in Colombia.