Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : Gurus for XML TextStream vs Response Stream vs XMLValidatingReader


Neal
2/18/2005 6:15:01 AM
Hi,

I try retrieve an XML stream from a webapp
using a post
"http://webedu.its.uct.ac.za/webct/public/serve_webctdb?OPERATION=homearea_xml&DB=global&WebCT%20ID=rodgersn_its_main&AUTH=ee09aabc77ef593917a60720f167c733"

and
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()

from here ..I try read the XML retrieved, but...

I've tried XMLStream and XMLValidatingStream..both return
"There is invalid data at the root level"

Trying a std StreamReader with say .readtoend or
sr.Read(read, 0, 256) in a count loop, I get an error "too few parameters"
on the
string result of the read.

Where do I go from here?
Tia
--
Neal Rogers
Martin Honnen
2/18/2005 4:22:29 PM


[quoted text, click to view]


[quoted text, click to view]

That is a URL with a query string so we only know the URL and any data
send with a HTTP GET request. When I load that URL with IE 6 which
parses the XML with MSXML 3 it is loaded without errors. Thus if you get

[quoted text, click to view]

errors with .NET then perhaps the server sends a different response
after your POST request, perhaps an HTTP response with an error code and
a text/html response body which the XML parser can't parse.
So we need to see exactly what you do to see what could go wrong.

[quoted text, click to view]

What language are you using? With VB.NET I think you need
Dim responseText As String
responseText = sr.ReadToEnd()
if you want to use a StreamReader.


--

Martin Honnen
Neal
2/21/2005 2:53:02 AM
Hi Martin

Yes, It is a VB.Net app

This is the full rtn's code..
Thanks
Neal

Dim result As WebResponse
Dim req As WebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder()
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}

req = WebRequest.Create(url)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded" 'The
ContentType property contains the media type of the request

Dim strResult As String

Try
Dim i As Integer = 0
If payload <> Nothing Then
Dim j As Integer


While i < payload.Length
j = payload.IndexOfAny(reserved, i)
If j = -1 Then

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length -
i)))
Exit While
End If

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i)))
UrlEncoded.Append(payload.Substring(j, 1))
i = j + 1
End While
SomeBytes =
System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()
Else
req.ContentLength = 0
End If

'-- Response Object
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream, encode)


Dim xr As New XmlTextReader(New StreamReader(ReceiveStream,
encode))
xr.Normalization = True
If xr.CanResolveEntity Then
xr.ReadStartElement()
strResult = strResult & xr.Value
End If

' 'For DTD....
' Dim vreader As New XmlValidatingReader(xr)
' Dim resolver As New XmlUrlResolver()
' vreader.XmlResolver = resolver

' If vreader.CanResolveEntity Then
' Dim attrCnt As Integer = vreader.AttributeCount
' Dim xBaseUri As String = vreader.BaseURI.ToString
' Dim yLocalNodeName As String = vreader.LocalName.ToString
' Dim sNdType As String = vreader.NodeType.ToString


' 'While vreader.read 'reads the next node from the
stream
' strResult = strResult & vreader.ReadInnerXml() '=
nothing
' strResult = strResult & vreader.Value
'' strResult = strResult & vreader.
' 'End While

' While xr.Read
' strResult = strResult & xr.ReadInnerXml 'str
' 'error: there is invalid data at the root level,
line 1,position 1
' End While
' End If
Catch Exc As Exception
Throw New Exception(Exc.Message)
Finally
If Not result Is Nothing Then
result.Close()
End If
If Not RequestStream Is Nothing Then
RequestStream.Close()
End If
If Not ReceiveStream Is Nothing Then
ReceiveStream.Close()
End If
End Try
Return strResult





[quoted text, click to view]
AddThis Social Bookmark Button