2

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

4 Answers 4

7

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
Sign up to request clarification or add additional context in comments.

6 Comments

'GetResponse' is not a member of 'System.Net.HttpWebRequest'.
@ScottBeeson In which programming language? For .NET, System.Net.HttpWebRequest.GetResponse() has been around since version 1.1, see msdn.microsoft.com/en-us/library/…
vb.net 4.5, Visual Studio 2012
I believe you, I'm just telling you what the IDE is telling me :)
|
3

Along with what @Jon Skeet said there's also the built-in 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

1 Comment

2

Have you tried using XDocument.Load or XmlDocument.Load?

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

Comments

1

try this

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...

Comments

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.