如何从 Soap Web Response 获取元素数据?网络 [英] How to get element data from Soap Web Response? VB.NET

查看:32
本文介绍了如何从 Soap Web Response 获取元素数据?网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网络服务获取数据,只返回一个结果,即库存中给定商品的数量.

I'm trying to get data from a webservice, returning just one result, the number of a given item in stock.

我成功得到结果,但需要从中剥离所有 XML 代码以简单返回数字,返回的 XML 如下所示:

I'm successfully getting a result, but need to strip all the XML code from it to simple return the number, the XML being returned looks like:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <stockenquiryResponse xmlns="https://webservices.electrovision.co.uk">
      <stockenquiryResult>**THE NUMBER I NEED**</stockenquiryResult>
    </stockenquiryResponse>
  </soap:Body>
</soap:Envelope>

我确定这已经被问过很多次了,但我找不到一个简单的解决方案来从 stockenquiryresult 标签中获取值.

I'm sure this has been asked many times, but I can't find a simple solution to just get the value from the stockenquiryresult tag.

从 vbnet 中的 XML 获取值

似乎是正确的答案,但我无法让它发挥作用.

Seems like the right answer, but I can't get it to work.

如果有帮助,我将使用以下示例获取数据:

If it helps, I am getting the data using the example from :

http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.80).aspx

只需进行一些调整即可正确获取数据,最显着的是将内容类型更改为 application/soap+xml 并将数据作为 XML 传递.

With just a couple of tweaks to get the data correctly, most notably changing the content type to application/soap+xml and passing the data as XML.

我在 ASP.NET 2.0 中使用 VB.

I am using VB with ASP.NET 2.0.

推荐答案

有一些内置的 .NET 类可用于读取 XML.

There are some built in .NET classes that you can use to read XML.

使用 XmlDocument

XmlDocument 在 DOM(文档对象模型)中公开您从 Web 服务中检索到的 XML 字符串.您可以在 MSDN 上阅读有关 XmlDocument 的信息.

XmlDocument exposes the XML string you retrieved from the web service in a DOM (Document Object Model). You can read about XmlDocument on MSDN.

Dim XMLDoc as new XMLDocument

XMLDoc.LoadXML(XMLString)

Dim Result as string = XMLDoc.LastChild.InnerText

'Alternatively, you can use SelectSingleNode.
'You will need to determine the correct XPath expression.
'Dim Result as string = XMLDoc.SelectSingleNode("XPathToNode").InnerText

如果您选择使用 SelectSingleNode,MSDN 上的 XPath 文档会派上用场.

If you choose to use SelectSingleNode, the XPath documentation on MSDN will come in handy.

使用 XmlReader

对于阅读一个标签这样快的事情,您还可以使用 XmlReader (MSDN 文档).与 XmlDocument 不同,XmlReader 不会将 XML 作为 DOM 公开.XmlReader 是一个前向阅读器,但应该比 XmlDocument 更快、更轻量.这适用于像您这样的情况.

For something as quick as reading one tag, you may also use an XmlReader (MSDN Documentation). Unlike XmlDocument, XmlReader does not expose the XML as a DOM. XmlReader is a forward only reader, but should be faster and more lightweight than XmlDocument. This works fine for situations such as yours.

Dim XSettings as new XmlReaderSettings
'You can use XSettings to set specific settings on the XmlReader below.
'See linked docs.

Using SReader as New StringReader(XMLString)

    Dim X as XmlReader = XmlReader.Create(SReader, XSettings)
    X.ReadToDescendant("stockenquiryResult")
    Dim Result as string = X.ReadElementContentAsString

End Using

这篇关于如何从 Soap Web Response 获取元素数据?网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆