Web Service in Windows Form C#

Hello

I have added a web service to my windows project. The service is one that you can use to take a zip code and return the City and State.

I have on my form 3 text boxes 1 for Zip, City and State and a button that I want to use to run the process that will take the zip in txtboxzip and return the city and state.

I am using the web service
http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP

I have tried several ways of doing this. I think I am close, but always an error like the last.

"cannot implicitly convert type 'System.xml.xmlnode' to 'string'"

Any help would be great.
riley00Asked:
Who is Participating?
 
Robert SchuttSoftware EngineerCommented:
Try using something like this:
            ServiceReference1.USZipSoapClient z = new ServiceReference1.USZipSoapClient("USZipSoap");
            XmlNode x = z.GetInfoByZIP(textBox1.Text);
            if (x.HasChildNodes) {
                textBox2.Text = x.SelectSingleNode("//CITY").InnerText;
                textBox3.Text = x.SelectSingleNode("//STATE").InnerText;
            } else {
                textBox2.Text = "N/A";
                textBox3.Text = "N/A";
            }

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
If you want something a bit more raw, that doesn't require adding a service reference, you can use:
            string url = string.Format(
                "http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip={0}",
                ZIPText.Text
                );

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());

            XDocument doc = XDocument.Parse(sr.ReadToEnd());

            var data = (from n in doc.Root.Elements("Table")
                    select new {
                        City = (string)n.Element("CITY"),
                        State = (string)n.Element("STATE")
                    }).Single();

            CityText.Text = data.City;
            StateText.Text = data.State;

Open in new window

0
 
riley00Author Commented:
Awesome, Thanks for your help!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.