[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Consuming a Web Service XML using C#

Posted on 2006-03-29
Medium Priority
Last Modified: 2011-09-20
I am trying to make use of a Zip Code Web Service which recieves a 5 digit zip code and returns City, State, Area Code, etc...

The web service is located here: http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP

In Visual Studio 2003 (using c#) I have setup the Web Reference, added a text box, and called the Web Service on a button click no problem.

The problem is what to do with the information that I get back.  From the error messages I have been recieving (when I don't add a .ToString()) it seems that the information being returned to me is in the format System.Xml.XmlNode

The gist of my code is something like: lblMsg.Text = zip_code.GetInfoByZIP(tbZipCode.Text).InnerXml;

using InnerXml gets me all the information, but all smushed together ala "Beverly HillsCA90210310P"

I need to be able to do two things with this input
1. Bind the XML to a DataSet
2. Read each piece/node of the XML seperatley


Question by:Argblat
  • 3
  • 2
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16327573

The best thing to do is to create a dataset and then you can use ReadXml() and if you have the data in a string that you get back then you can use a StringReader to read the string into the Dataset and have it create the table.

When you have that then the dataset can be referenced by Tables[0][Columnname], etc.

Let me know if I can help more.
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16327576
I meant to say, You can do, not necessarily the Best thing to do.

Author Comment

ID: 16384694

Sorry for the delay in responding...I have been away with a family issue.

Would it be possible for you to please supply some code to show me exactly what you are talking about?

LVL 25

Accepted Solution

DBAduck - Ben Miller earned 2000 total points
ID: 16389557
You can use a DataSet to do both items that you are looking to do.

using System.IO;
using System.Data;


string xml = zip_code.GetInfoByZIP(tbZipCode.Text).InnerXml;

StringReader sr = new StringReader(xml);

DataSet ds = new DataSet();

string zipCode = ds.Tables[0]["ZIP"].ToString();


Is that what you are looking for?  And you can also bind this to a datagrid or whatever, by using the DataSet that gets created in the ReadXml();


Author Comment

ID: 16404335
Muchos Gracias dbaduck,

That's exatly what I was looking for to bridge the gap between the xml feed and making use of the data in .net

One Correction for prosperties sake:
string zipCode = ds.Tables[0]["ZIP"].ToString();

didn't seem to be working, but this fixes the problem:

string zipCode = ds.Tables[0].Rows[0]["ZIP"].ToString();


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question