Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload