Consuming XML Web Service

I have a weather web service that I am trying to get working on my page. I have added a web reference successfuly and am now trying to code it. It is a very simple web service that supplies me city names based on a country that I choose.

The web service exposes a function called GetCitiesByCountry. I have a list box (no particular reason why) that I want to populate with the cities. I have textbox I type the countries name in, and a button to generate the results. I have the following code in my buttons Click function:

        'connect to the web service
        Dim aw As weatherservice.GlobalWeather
        aw = New weatherservice.GlobalWeather

        txt.DataSource = aw.GetCitiesByCountry(txtCountry.Text)
        txt.DataBind()
        txt.Visible = True

The results I get in my listbox look like this:
<
N
e
w
D
a
t
a
S
e
t
>
 and it carries on vertically with the following: <Table><Country>United Kingdom</Country><City><Belfast</City> etc etc

Any ideas why?


Craig_MucklestonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HavaganCommented:
Web services typically return XML and not a .NET object which can be directly bound to a web control. Instead, you have to consume the XML using something like:

Dim ds as New DataSet
ds.ReadXml(aw.GetCitiesByCountry(txtCountry.Text))

You can then bind the DataSet (or DataTables in the DataSet) to your web controls.

Paul
dba_netanyaCommented:
What does GetCitiesByCountry method return? DataSet?
Craig_MucklestonAuthor Commented:
Hi Havagan,

I have tried your way but get the following error:

System.IO.PathTooLongException: The path is too long after being fully qualified...
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

HavaganCommented:
Craig,

That's because the example I gave you was just a generic blub to point out that you need to consume the XML into a .NET object. The web service author should provide an API telling you exactly what the web service is returning and how it is to be consumed. A simple example for a web service which returns a dataset can be consumed by using the following:

Dim ds As DataSet = aw.GetCitiesByCountry(txtCountry.Text)

But other web services (such as Amazon) may actually return a custom object that you must declare. Do you have the URL for the web service you're consuming?

Paul
HavaganCommented:
If this is the web service you're using:

http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry

The above code snippet should work.

Paul
jyotisinhaCommented:
the return type of Web method could be either Dataset or collection or any object that's bindable to Dropdown list. You need to typecast suitably before binding the object to dropdownlist.

something like this (here I'm assuming returntype is dataset)-

Dim aw As weatherservice.GlobalWeather
        aw = New weatherservice.GlobalWeather

        txt.DataSource = Ctype(Dataset)aw.GetCitiesByCountry(txtCountry.Text)
        txt.DataBind()
        txt.Visible = True

if object itself is not bindable(say XML file) then you need to convert this into some bindable object say Dataset before binding to dropdownlist.


HTH,
Jyoti.

Craig_MucklestonAuthor Commented:
The above web service is exactly the one I am trying to consume.

Does that mean then the following code should work?

        Dim aw As New weatherservice.GlobalWeather
        Dim ds As DataSet = New DataSet(aw.GetCitiesByCountry(txtCountry.Text))

        ddl.DataSource = ds
        ddl.DataBind()

(sorry - I'm new to web services)
HavaganCommented:
That web service returns a String object which must be converted in a slightly different way. You can't convert a string to a dataset using CType() or DirectCast() so you have to use a StringReader object as an intermediary:

        'return XML as a string from the web service
        Dim xml As String = ws.GetCitiesByCountry("Germany")

        'read the XML string into a string reader
        Dim sr As New System.IO.StringReader(xml)

        'instantiate a dataset from the streamreader
        Dim ds As New DataSet
        ds.ReadXml(sr)

        'sort the results alpha by City name
        ds.Tables(0).DefaultView.Sort = "City"

        'bind the dataset to the drop down list
        'which has a single table with two columns: Country and City

Paul


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.