1

I am learning asp.net, and trying to understand in consuming a webservice. I am using the following wsdl file

http://www.webservicex.net/uszip.asmx?WSDL

I am using a textbox to enter the zipcode, added the wsdl in the web reference , and am using C# to retrieve data. The structure of the data is as follows:

http://www.webservicex.net/uszip.asmx/GetInfoByZIP

I am not able to understand on how to display the results on a webpage. The following code is what I have until now..

protected void Button1_Click(object sender, EventArgs e)
{
    USZip s1 = new USZip();
    var input = zipcode.Text.ToString();
    String result = s1.GetInfoByZIP(input);

}

It would be great if anyone can give me helpful pointers

Thanks a lot

2 Answers 2

2

Hover over the call to GetInfoByZIP and you should see that it returns an XmlNode. Since there is no implicit conversion from an XmlNode to a string, then you should not be able to assign the result directly to a string. To get the string value, call the XmlNode property OuterXml, like so (make sure you have a multi-line text control):

XmlNode result = s1.GetInfoByZIP(input);    
YourTextControl.Text = result.OuterXml;

Per the WSDL, the call returns a GetInfoByZipResult, which can contain any XML (with no further specification in the WSDL). The XML I get looks like the XML below. So, if you want to get to the name of the City, State, etc., you will need to parse the XML.

<NewDataSet xmlns="">
  <Table>
    <CITY>Glendale</CITY> 
    <STATE>CA</STATE> 
    <ZIP>91210</ZIP> 
    <AREA_CODE>818</AREA_CODE> 
    <TIME_ZONE>P</TIME_ZONE> 
  </Table>
</NewDataSet>
Sign up to request clarification or add additional context in comments.

Comments

0

You can either assign result to a control's Text property (such as a Literal, or a Label), or you can use Response.Write(). Assigning it to a control is a better way to go, as just pumping it out to the response stream probably isn't going to get it displayed where and how you want.

In your aspx page you should add a control:

<asp:Literal Id="postCodeInfo" runat="server"></asp:Literal>

And at the end of your Button1_Click event, assign result to that literal's Text property:

postCodeInfo.Text = result;

Also on the second line, I'm assuming that zipcode.Text is a string, so you don't need to call ToString() on it again (if it isn't a string, maybe you do).

1 Comment

Thanks for the reply. I think the result is a XML structure. It shows 'Cannot implicitly convert type System.Xml.XmlNode to string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.