How to return IEnumerable from XML web services

Hi ,

This post gives you an idea how to return IEnumerable object in your XML web services, the tip for this post is how to convert this IEnumerable object to something that can be serialized in your xml web serivces.

If you are using LINQ in your xml web serivces, below is an example for the signature of your xml web service:

[WebMethod]
public IEnumerable ListUser()
{
return logic.ListUser();
}


If you run this xml web service, you will get this error :

“Cannot serialize interface System.Collections.Generic.IEnumerable”

To solve this problem, simply convert the IEnumerable Object to Array using ToArray() extension method.

[WebMethod]
public TwitterUser[] ListUser()
{
return logic.ListUser().ToArray();
}


This will fix this problem and return the result as XML in your xml web service.


Hope this helps.

Regards,
Mostafa arafa

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>