如何从 vb.net 中的远程 url 读取 xml [英] how to read xml from remote url in vb.net

查看:43
本文介绍了如何从 vb.net 中的远程 url 读取 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中工作,我想从远程 url 读取一些数据,任何人都可以帮助我如何执行此功能

解决方案

您可以使用 WebRequest 从远程站点检索 XML;然后您可以将内容解析为 XmlDocument 对象.

<前>' 创建一个到远程站点的 WebRequestDim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.domain.com/fetch.xml")'注意!仅当网站受到保护时才使用以下行request.Credentials = New System.Net.NetworkCredential("username", "password")' 调用远程站点,并解析响应对象中的数据Dim response As System.Net.HttpWebResponse = request.GetResponse()' 检查响应是否正常(状态码 200)如果 response.StatusCode = System.Net.HttpStatusCode.OK 那么' 将响应中的内容解析为流对象Dim 流作为 System.IO.Stream = response.GetResponseStream()' 为流对象创建一个读取器Dim reader As New System.IO.StreamReader(stream)' 使用读取器从流对象中读取,将内容放入字符串中Dim 内容 As String = reader.ReadToEnd()' 创建一个新的空 XML 文档Dim document As New System.Xml.XmlDocument()' 将内容加载到 XML 文档中文档.LoadXml(内容)' 现在您有一个包含来自远程站点的 XML 的 XmlDocument 对象,您可以' 使用 System.Xml 命名空间中的对象和方法来读取文档别的' 如果对远程站点的调用失败,您将不得不处理这个问题.可能有很多原因,即.这' 远程站点没有响应(代码 404)或您的用户名和密码不正确(代码 401)'' 查看 System.Net.HttpStatusCode 枚举器中的代码抛出新异常(无法从 URL 检索文档,响应代码:" & response.StatusCode)万一

i am working in a project in that i want to read some data from the remote url can any one help me how to do this function

解决方案

You can use a WebRequest to retrieve the XML from the remote site; then you can parse the contents into an XmlDocument object.

' Create a WebRequest to the remote site
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.domain.com/fetch.xml")

' NB! Use the following line ONLY if the website is protected
request.Credentials = New System.Net.NetworkCredential("username", "password")

' Call the remote site, and parse the data in a response object
Dim response As System.Net.HttpWebResponse = request.GetResponse()

' Check if the response is OK (status code 200)
If response.StatusCode = System.Net.HttpStatusCode.OK Then

    ' Parse the contents from the response to a stream object
    Dim stream As System.IO.Stream = response.GetResponseStream()
    ' Create a reader for the stream object
    Dim reader As New System.IO.StreamReader(stream)
    ' Read from the stream object using the reader, put the contents in a string
    Dim contents As String = reader.ReadToEnd()
    ' Create a new, empty XML document
    Dim document As New System.Xml.XmlDocument()

    ' Load the contents into the XML document
    document.LoadXml(contents)

    ' Now you have a XmlDocument object that contains the XML from the remote site, you can
    ' use the objects and methods in the System.Xml namespace to read the document

Else
    ' If the call to the remote site fails, you'll have to handle this. There can be many reasons, ie. the 
    ' remote site does not respond (code 404) or your username and password were incorrect (code 401)
    '
    ' See the codes in the System.Net.HttpStatusCode enumerator 

    Throw New Exception("Could not retrieve document from the URL, response code: " & response.StatusCode)

End If

这篇关于如何从 vb.net 中的远程 url 读取 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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