I have a couple of questions regarding Web Services, and I would really appreciate if someone could point me in the right direction.
In my class library, I have a two classes: Hotel & RoomType. Hotel class contains an ArrayList of RoomType objects.
In my Web Service, I have a GetHotels method as follows:
[WebMethod]
[XmlInclude(typeof(Hotel))]
[XmlInclude(typeof(RoomType))]
public ArrayList GetHotels()
{
return Sistema.GetInstance().GetHotels();
}
GetHotels() in class Sistema, retrieves the information from the database and returns an ArrayList.
I had to use XMLInclude because I was getting:
The type Hotel (or RoomType) was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically
Then in my Web app I have this code:
WebService sample = new Service();
ArrayList hotels = service.GetHotels();
This doesn't compile, so I had to change to the following code:
WebService sample = new Service();
object[] hotels = service.GetHotels();
Here's my first question: Is it possible to return an ArrayList, or every time I will have to cast the result to an ArrayList?
Knowing that the ArrayList contains Hotel objects, I added the following code:
foreach (Hotel hotel in hotels)
{
...
}
This compiles, but when I execute, I get the following error:
Unable to cast object of type 'System.Xml.XmlNode[]' to type 'Hotel'.
So, my next question is: How can I cast the result to a Hotel object and work with it?