Consuming a Web Service XML using C#

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

-Mike

LVL 5
ArgblatAsked:
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.

DBAduck - Ben MillerPrincipal ConsultantCommented:
<NewDataSet>
<Table>
  <CITY>Lehi</CITY>
  <STATE>UT</STATE>
  <ZIP>84043</ZIP>
  <AREA_CODE>801</AREA_CODE>
  <TIME_ZONE>M</TIME_ZONE>
</Table>
</NewDataSet>

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.
0
DBAduck - Ben MillerPrincipal ConsultantCommented:
I meant to say, You can do, not necessarily the Best thing to do.
0
ArgblatAuthor Commented:
dbaduck,

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?

-Mike
0
DBAduck - Ben MillerPrincipal ConsultantCommented:
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();
ds.ReadXml(sr);

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

etc.

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();

Ben.
0

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
ArgblatAuthor Commented:
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();

-Mike
0
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.

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.