VBScriptor

Hi, I'm building an application that uses an XML file from the web.

Dim sr As New System.IO.StreamReader("http://site.com/file.xml")

In that format. However, at compile, I get an error. Apparently, StreamReader only works for LOCAL files, not with URI's. How can I make this work I can post more info if needed.



Re: Visual Basic Express Edition Using System.IO.StreamReader to get XML files from the web

VBScriptor

I really hate to bump this, but it's on the second page and still no answer Sad Anyone





Re: Visual Basic Express Edition Using System.IO.StreamReader to get XML files from the web

Wade73

I believe you are going to want to use the httpwebrequest object to get the stream. Let me look for an example I have (on my work pc), but I think that it will do what you are looking for. Just look up httpwebrequest on msdn.microsoft.com for more information.

[Update]

Here is a link to more the code I used from the Microsoft MSDN website - http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(VS.71).aspx and here is the code

Code Snippet
Imports SystemImports System.Net
Imports System.TextImports System.IO


Public Class Test

' Specify the URL to receive the request.
Public Shared Sub Main(ByVal args() As String)
Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)


' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4

' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials

Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

Console.WriteLine("Content length is {0}", response.ContentLength)
Console.WriteLine("Content type is {0}", response.ContentType)

' Get the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()

' Pipes the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)

Console.WriteLine("Response stream received.")
Console.WriteLine(readStream.ReadToEnd())
response.Close()
readStream.Close()
End Sub 'Main
End Class 'Test
'
'The output from this example will vary depending on the value passed into Main
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'<html>
'...
'</html>
'
'


Wade





Re: Visual Basic Express Edition Using System.IO.StreamReader to get XML files from the web

VBScriptor

Thanks! I'm looking it up as we speak. I'll see what I can find out. I'm somewhat new to VB.NET, but I have background in VB6 (a bit), and mostly web languages (PHP, JS, etcetera) so I understand the concepts. I just don't have all the classes and namespaces down yet Stick out tongue