Consuming a Web Service XML using C#

Posted on 2006-03-29
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:

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
    LVL 24

    Expert Comment

    by:DBAduck - Ben Miller

    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 24

    Expert Comment

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

    Author Comment


    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 24

    Accepted Solution

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

    LVL 5

    Author Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    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.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now