Read Xml data

M

Martyn Fewtrell

I am having a problem reading an Xml file and loading it into a dataset in
VB.net (ASP).

I seem to be able to load the remote file from the Url but once in memory I
cant manage to load it in to a dataset.

The readXml appears to want to read a file rather than a variable and I'm
not sure how to get around this issue.

I've tried loading to a file stream and converting the strFile to an
XmlDocument but this doesn't appear to get me any further.

I known the code at the bottom doesn't work I have listed it to put over the
idea of what I am trying to do.

Martyn Fewtrell
(e-mail address removed)

--------------------------------------------
'get data

Dim Url As String = "http://www.url/file.xml"

Dim Request As HttpWebRequest

Dim Response As WebResponse

Dim strFile As String

Try

Request = CType(WebRequest.Create(Url), HttpWebRequest)

Response = Request.GetResponse()

litError1.Text = "Response Recieved"

Catch ex As Exception

litError1.Text = "Response Not Recieved"

End Try

'load Dataset

Dim ds As DataSet = New DataSet

ds.ReadXml(strData)

----------------------------------------------------------------------------
 
J

John Saunders

Martyn Fewtrell said:
I am having a problem reading an Xml file and loading it into a dataset in
VB.net (ASP).

I seem to be able to load the remote file from the Url but once in memory I
cant manage to load it in to a dataset.

The readXml appears to want to read a file rather than a variable and I'm
not sure how to get around this issue.

I've tried loading to a file stream and converting the strFile to an
XmlDocument but this doesn't appear to get me any further.

I known the code at the bottom doesn't work I have listed it to put over the
idea of what I am trying to do.

Martyn,

The first thing you need to do is get something out of the WebResponse. If
you look at the documentation for WebResponse, you'll see that there is a
WebResponse.GetResponseStream method which returns a Stream.

Next, if you look at the documentation for DataSet.ReadXml, you'll see that
there is an overload which accepts a Stream as input. Putting this together,
you get:

'get data

Dim Url As String = "http://www.url/file.xml"
Dim Request As WebRequest
Dim Response As WebResponse
Dim strFile As String

Dim ds As DataSet = New DataSet

Try
Request = WebRequest.Create(Url)
Response = Request.GetResponse()
litError1.Text = "Response Recieved"

Dim responseStream As Stream = Response.GetResponseStream()
'load Dataset
ds.ReadXml(responseStream)
Catch ex As Exception
litError1.Text = "Response Not Recieved"
End Try
 
M

Martyn Fewtrell

John Saunders said:
memory

Martyn,

The first thing you need to do is get something out of the WebResponse. If
you look at the documentation for WebResponse, you'll see that there is a
WebResponse.GetResponseStream method which returns a Stream.

Next, if you look at the documentation for DataSet.ReadXml, you'll see that
there is an overload which accepts a Stream as input. Putting this together,
you get:

'get data

Dim Url As String = "http://www.url/file.xml"
Dim Request As WebRequest
Dim Response As WebResponse
Dim strFile As String

Dim ds As DataSet = New DataSet

Try
Request = WebRequest.Create(Url)
Response = Request.GetResponse()
litError1.Text = "Response Recieved"

Dim responseStream As Stream = Response.GetResponseStream()
'load Dataset
ds.ReadXml(responseStream)
Catch ex As Exception
litError1.Text = "Response Not Recieved"
End Try
Thanks John

I shall try this this evening and see how it all fits together!

Martyn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top