By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,797 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,797 IT Pros & Developers. It's quick & easy.

Help streaming XML instead of dataset

P: n/a
JJA
I am confused about all the various methods of handling XML in ASP.Net
2.0. I want to be able to stream XML to some clientside Javascript code
(snippet below):

var urlRequest = "../GetLendersAsXML.aspx"
var request = GXmlHttp.create();
request.open("GET", urlRequest, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
var xmlDoc = request.responseXML;

I have all the Javascript code I need but I need to have the page above
return XML.

Using SQL 2005 I have created an HTTP endpoint and exposed a stored
procedure as a webservice. I want to add a FOR XML AUTO clause in the
stored procedure to return XML as a big string (that part is easy). The
sproc currently returns a dataset and I've got this code below that
binds it to a GridView (this works but is not what I need):

Partial Class GetLendersAsDataset
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Try
Dim ws As yukon.ITMO_Lenders = New yukon.ITMO_Lenders
Dim ds As System.Data.DataSet
ws.Credentials = New System.Net.NetworkCredential("myID",
"myPass", "myDomain")
Dim oa As Object()
oa = ws.GetLenderLocations("GOLDEN EMPIRE MORTGAGE")
'hardcode example for testing
If oa(1).ToString() = "System.Data.DataSet" Then
ds = DirectCast(oa(1), System.Data.DataSet)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End If
Catch ex As ApplicationException
Response.Write(ex.ToString)
End Try
End Sub
End Class

So, if I change my stored procedure by adding FOR XML clause, then
ws.GetLenderLocations webmethod above will return XML. Can anybody
suggest code for my ASPX page to stream this XML so that my Javascript
at the top of the post can read it?

Dec 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have you tried doing a this.Response.Write(...) in GetLendersAsXML.aspx's
OnLoad(...) override?
Dec 1 '06 #2

P: n/a
JJA
Thank you so much for responding. I've been researching all day on this
and the problem has morphed a bit now. First, I am trying something
close to what you suggest (i.e. I'm trying Response.OutputStream.Write
- see below).

In summary, how to convert System.Xml.XmlElement to an output stream or
byte array?

See: http://www.developer.com/db/article.php/10920_3390241_3
for a pretty good explanation of what is returned and what I am dealing
with. By adding FOR XML to my stored proc, the type returned in
oa(1).ToString is ""System.Xml.XmlElement". Removing it causes
oa(1).ToString to be equal to "System.Data.DataSet".

But I am still confused as to how to pump that data out directly. Here
is my latest attempt (but only the 2nd case for dataset works). In
short, how to convert a big chunk of type system.xml.xmlelement to byte
array or something that can be streamed out?
Here's my code:

Partial Class GetLendersAsDataset
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Try
Dim ws As yukon.ITMO_Lenders = New yukon.ITMO_Lenders
ws.Credentials = New System.Net.NetworkCredential("myID",
"myPass", "myDomain")
Dim oa As Object()
oa = ws.GetLenderLocations("GOLDEN EMPIRE MORTGAGE")
If oa(1).ToString() = "System.Xml.XmlElement" Then
Dim i As Integer
Dim xd As System.Xml.XmlElement
Response.ContentType = "text/xml"
xd = DirectCast(oa(1), System.Xml.XmlElement)
Dim encoding As New System.Text.ASCIIEncoding()
Dim oaCopy As Object
oaCopy = oa(1)
i = encoding.GetBytes(oaCopy)
'<---------------------blows up on this
stmt (see Server Error below)
Response.OutputStream.Write(encoding.Get
Bytes(oaCopy), 0, i)
Response.Flush()
Response.End()
End If
If oa(1).ToString() = "System.Data.DataSet" Then 'This
case works fine:
Dim ds As System.Data.DataSet
ds = DirectCast(oa(1), System.Data.DataSet)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End If
Catch ex As ApplicationException
Response.Write(ex.ToString)
End Try
End Sub
End Class

Server Error in '/LenderLocations' Application.
--------------------------------------------------------------------------------

Overload resolution failed because no Public 'GetBytes' can be called
with these arguments:
'Public Overrides Function GetBytes(s As String) As Byte()':
Argument matching parameter 's' cannot convert from 'XmlElement'
to 'String'.
'Public Overrides Function GetBytes(chars As Char()) As Byte()':
Argument matching parameter 'chars' cannot convert from
'XmlElement' to 'Char()'.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Reflection.AmbiguousMatchException: Overload
resolution failed because no Public 'GetBytes' can be called with these
arguments:
'Public Overrides Function GetBytes(s As String) As Byte()':
Argument matching parameter 's' cannot convert from 'XmlElement'
to 'String'.
'Public Overrides Function GetBytes(chars As Char()) As Byte()':
Argument matching parameter 'chars' cannot convert from
'XmlElement' to 'Char()'.

Source Error:
Line 15: Dim oaCopy As Object
Line 16: oaCopy = oa(1)
Line 17: i = encoding.GetBytes(oaCopy)
Line 18:
Response.OutputStream.Write(encoding.GetBytes(oaCo py), 0, i)
Line 19: Response.Flush()

Dec 1 '06 #3

P: n/a
If you want to stream it out, I'd recommend serializing it, but in reality,
your XML is basically a serialized object anyway (once it's correctly
"serialized" all the encoding is based on the runtime's settings so you can
leave them alone), so you'd be taking a serialized object (the element),
deserializing it (into an object) and then reserializing it (to send it over
the response). If the XML you get from the DB is a complete & correct XML
document, you can just stream the XML directly without doing any encoding

The particular error you are getting is caused by you passing in an
XmlElement to GetBytes rather than the expected string. That can be
resolved by replacing:
encoding.GetBytes(oa)

with:
encoding.GetBytes(oa.OuterXml)

Dec 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.