如何从 vb.net 中的远程 url 读取 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3757731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:02:39  来源:igfitidea点击:

how to read xml from remote url in vb.net

vb.net

提问by Aundykarthick

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

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

回答by KBoek

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

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

' 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

回答by Chris Haas

Along with what @Jon Skeet said there's also the built-in WebClient:

除了@Jon Skeet 所说的,还有内置的WebClient

    Dim MyData As String
    Try
        Using WC As New System.Net.WebClient()
            MyData = WC.DownloadString("http://www.example.com/text.xml")
        End Using
    Catch ex As Exception
        'Error downloading
    End Try

回答by Jon Skeet

Have you tried using XDocument.Loador XmlDocument.Load?

你试过使用XDocument.LoadXmlDocument.Load吗?

If those don't do what you want, please give more details.

如果那些没有做你想要的,请提供更多细节。

回答by Nitron

try this

尝试这个

http://www.codeproject.com/Tips/992109/Parsing-Reading-XML-from-URL-in-VB-NET

http://www.codeproject.com/Tips/992109/Parsing-Reading-XML-from-URL-in-VB-NET

There are some useful examples provided with it. Hope this helps...

它提供了一些有用的示例。希望这可以帮助...