使用 asp.net 和 C# 使用 Web 服务
我正在学习 asp.net,并试图了解如何使用网络服务。我正在使用以下 wsdl 文件
http://www.webservicex.net/uszip.asmx?WSDL< /a>
我使用文本框输入邮政编码,在 Web 参考中添加 wsdl,并使用 C# 检索数据。数据结构如下:
http://www.webservicex.net/uszip.asmx/ GetInfoByZIP
我无法理解如何在网页上显示结果。以下代码是我到目前为止所拥有的。
protected void Button1_Click(object sender, EventArgs e)
{
USZip s1 = new USZip();
var input = zipcode.Text.ToString();
String result = s1.GetInfoByZIP(input);
}
如果有人能给我有用的指示,那就太好了
,非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将鼠标悬停在对 GetInfoByZIP 的调用上,您应该看到它返回一个 XmlNode。由于没有从 XmlNode 到字符串的隐式转换,因此您不应该能够将结果直接分配给字符串。要获取字符串值,请调用 XmlNode 属性 OuterXml,如下所示(确保您有一个多行文本控件):
根据 WSDL,调用返回 GetInfoByZipResult ,它可以包含任何 XML(WSDL 中没有进一步的规范)。我得到的 XML 类似于下面的 XML。因此,如果您想获取城市、州等名称,则需要解析 XML。
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):
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.
您可以将
result
分配给控件的Text
属性(例如 Literal 或 Label),也可以使用Response.Write()
。将其分配给控件是一种更好的方法,因为仅将其输出到响应流可能无法使其按您想要的方式显示。在您的 aspx 页面中,您应该添加一个控件:
并在
Button1_Click
事件的末尾,将result
分配给该文字的Text
属性:也可以第二行,我假设
zipcode.Text
是一个字符串,因此您不需要再次调用ToString()
(如果不是)一个字符串,也许你会这样做)。You can either assign
result
to a control'sText
property (such as a Literal, or a Label), or you can useResponse.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:
And at the end of your
Button1_Click
event, assignresult
to that literal'sText
property:Also on the second line, I'm assuming that
zipcode.Text
is a string, so you don't need to callToString()
on it again (if it isn't a string, maybe you do).