VB.Net - Getting data from streamreader

Hi Experts.     I have used the following code from MSDN and correctly get the xml file as a response.   I don't need the xml file as such but only need to get the values.  Is there a way of extracting any required data contained in the streamreader before it is written to the xml file.  Or maybe writing the xml file to memory and reading it from there.   Writing the xml file to disk, extracting the data and then deleting the file, seems to be a waste.  Many Thanks

' Create a 'WebRequest' object with the specified url
Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' Call method 'GetResponseStream' to obtain stream associated with the response object
Dim ReceiveStream As Stream = myWebResponse.GetResponseStream()

Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

' Pipe the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(ReceiveStream, encode)
Console.WriteLine(ControlChars.Cr + "Response stream received")
Dim read(256) As [Char]

' Read 256 charcters at a time    .
Dim count As Integer = readStream.Read(read, 0, 256)
Console.WriteLine("HTML..." + ControlChars.Lf + ControlChars.Cr)
While count > 0

    ' Dump the 256 characters on a string and display the string onto the console.
    Dim str As New [String](read, 0, count)
    count = readStream.Read(read, 0, 256)

End While

' Release the resources of stream object.

 ' Release the resources of response object.
Who is Participating?
Robert SchuttConnect With a Mentor Software EngineerCommented:
After this line:
Dim ReceiveStream As Stream = myWebResponse.GetResponseStream()

Open in new window

try this code instead of the rest that's there now:
        Dim xmlDoc As New System.Xml.XmlDocument

Open in new window

From that xml document (in memory) you can get any info you need, a good starting point if needed: the XmlDocument documentation.

Alternatively (mainly if it's a very large xml document) you can use:
Dim xr As New System.Xml.XmlTextReader(ReceiveStream)

Open in new window

But that needs a bit more effort to process, see the XmlTextReader documentation.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.