How to run through this and display it?

Baluba

Member
Joined
Dec 24, 2017
Messages
5
Programming Experience
Beginner
Hi,


I am having trouble figuring out how to use the response from a web service which returns a list of cities from a given country.


The Web Service I am using is --> http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry


When entering Ireland this is the result:


C#:
<string xmlns="http://www.webserviceX.NET">
<NewDataSet> <Table> <Country>Ireland</Country> <City>Cork Airport</City> </Table> <Table> <Country>Ireland</Country> <City>Dublin Airport</City> </Table> <Table> <Country>Ireland</Country> <City>Casement Aerodrome</City> </Table> <Table> <Country>Ireland</Country> <City>Shannon Airport</City> </Table> </NewDataSet>
</string>


I am trying to figure out how to run through this list and display it in a Windows Form Application.
The application is just a simple text field where I enter the name of the country, a button and a label which is suppose to show the cities.


I just can't figure out how to run over the list. Is there a clever way? I am just thinking about doing something with Regex or something like that, but there has to be a much easier and cleaner way of doing it. There has to be a way to list out the content between the city tags, but I just keep finding XML examples and when playing around with the examples where I create a Xmldocument and using the load method doesn't work for me.


A big problem is that I don't know what to Google for to find help or a solution.


Could you guys take a look at this and help me out by either showing me, pointing me in the right direction or tell me what to use / Google for?


C#:
private void button1_Click(object sender, EventArgs e)
{
    CitiesByCountryWebService.GlobalWeatherSoapClient cityClient = new CitiesByCountryWebService.GlobalWeatherSoapClient();

    var response = cityClient.GetCitiesByCountry(txtBxCountry.Text);
}
 
What you're getting back is XML data so you should do a bit of research on how to read XML in C#. From the content, it looks like you might simply be able to create a DataSet and call its ReadXml method and then treat it like any other DataSet, e.g. one that you've populated from a database using ADO.NET. That would likely be the easiest option, assuming that it works, unless you need to get the data in some other specific format.
 
What you're getting back is XML data so you should do a bit of research on how to read XML in C#. From the content, it looks like you might simply be able to create a DataSet and call its ReadXml method and then treat it like any other DataSet, e.g. one that you've populated from a database using ADO.NET. That would likely be the easiest option, assuming that it works, unless you need to get the data in some other specific format.

Thanks. :) I'll look up DataSet some more. It did come up sometimes when I was Googling for answers, but it didn't quite work as the example showed and I was thinking I was just searching for the wrong things and the solution would be something else.

The "String" before everything in the XML response from the Web Service threw me off. This made me assume this wasn't proper XML, since what I've seen before doesn't have String in the beginning from what I remember.

Here is what I ended up with. I have to keep working on it to get it formatted and see if I can't find a way to write it cleaner. I also don't quite understand everything I've done. Instead of calling .InnerText I was thinking I could access the nodes through name.
In my code I call the Foreach variable City since I get explicity get the city element, but what I thought I could do was call the name of the element/Node somehow - like if the foreach (XmlNode city in xmllist) said foreach (XmlNode node in xmllist) I could do Node.City or Node.Country.

C#:
       private void button1_Click(object sender, EventArgs e)
        {
            CitiesByCountryWebService.GlobalWeatherSoapClient cityClient = new CitiesByCountryWebService.GlobalWeatherSoapClient();

            var response = cityClient.GetCitiesByCountry(txtBxCountry.Text);


            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(response);


            XmlNodeList xmllist = xmldoc.GetElementsByTagName("City");
            
            StringBuilder cityresult = new StringBuilder();
            foreach (XmlNode city in xmllist)
            {
                cityresult.Append(city.InnerText + " - ");
            }


            lblResult.Text = cityresult.ToString();
        }
 
I've done very little with XML so I'm far from an expert. I'll have a go and see what I can come up with though. I know that there aren't many around to help at this time of year and I have some responsibilities myself, but I should be able to have a go at it in the next 24 hours.
 
Formatted you can see xml as a tree of nodes, here you see there are multiple Table nodes that each has a Country and City child:
HTML:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://www.webserviceX.NET">
   <NewDataSet>
      <Table>
         <Country>Ireland</Country>
         <City>Cork Airport</City>
      </Table>
      <Table>
         <Country>Ireland</Country>
         <City>Dublin Airport</City>
      </Table>
      <Table>
         <Country>Ireland</Country>
         <City>Casement Aerodrome</City>
      </Table>
      <Table>
         <Country>Ireland</Country>
         <City>Shannon Airport</City>
      </Table>
   </NewDataSet>
</string>
 
Back
Top Bottom