Help streaming XML instead of dataset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JJA

    #1

    Help streaming XML instead of dataset

    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("G ET", urlRequest, true);
    request.onready statechange = function() {
    if (request.readyS tate == 4) {
    var xmlDoc = request.respons eXML;

    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 GetLendersAsDat aset
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Try
    Dim ws As yukon.ITMO_Lend ers = New yukon.ITMO_Lend ers
    Dim ds As System.Data.Dat aSet
    ws.Credentials = New System.Net.Netw orkCredential(" myID",
    "myPass", "myDomain")
    Dim oa As Object()
    oa = ws.GetLenderLoc ations("GOLDEN EMPIRE MORTGAGE")
    'hardcode example for testing
    If oa(1).ToString( ) = "System.Data.Da taSet" Then
    ds = DirectCast(oa(1 ), System.Data.Dat aSet)
    GridView1.DataS ource = ds.Tables(0)
    GridView1.DataB ind()
    End If
    Catch ex As ApplicationExce ption
    Response.Write( ex.ToString)
    End Try
    End Sub
    End Class

    So, if I change my stored procedure by adding FOR XML clause, then
    ws.GetLenderLoc ations 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?

  • Keith Patrick

    #2
    Re: Help streaming XML instead of dataset

    Have you tried doing a this.Response.W rite(...) in GetLendersAsXML .aspx's
    OnLoad(...) override?


    Comment

    • JJA

      #3
      Re: Help streaming XML instead of dataset

      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.Output Stream.Write
      - see below).

      In summary, how to convert System.Xml.XmlE lement 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.Xm lElement". Removing it causes
      oa(1).ToString to be equal to "System.Data.Da taSet".

      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.xmle lement to byte
      array or something that can be streamed out?
      Here's my code:

      Partial Class GetLendersAsDat aset
      Inherits System.Web.UI.P age
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles Me.Load
      Try
      Dim ws As yukon.ITMO_Lend ers = New yukon.ITMO_Lend ers
      ws.Credentials = New System.Net.Netw orkCredential(" myID",
      "myPass", "myDomain")
      Dim oa As Object()
      oa = ws.GetLenderLoc ations("GOLDEN EMPIRE MORTGAGE")
      If oa(1).ToString( ) = "System.Xml.Xml Element" Then
      Dim i As Integer
      Dim xd As System.Xml.XmlE lement
      Response.Conten tType = "text/xml"
      xd = DirectCast(oa(1 ), System.Xml.XmlE lement)
      Dim encoding As New System.Text.ASC IIEncoding()
      Dim oaCopy As Object
      oaCopy = oa(1)
      i = encoding.GetByt es(oaCopy)
      '<---------------------blows up on this
      stmt (see Server Error below)
      Response.Output Stream.Write(en coding.Get
      Bytes(oaCopy), 0, i)
      Response.Flush( )
      Response.End()
      End If
      If oa(1).ToString( ) = "System.Data.Da taSet" Then 'This
      case works fine:
      Dim ds As System.Data.Dat aSet
      ds = DirectCast(oa(1 ), System.Data.Dat aSet)
      GridView1.DataS ource = ds.Tables(0)
      GridView1.DataB ind()
      End If
      Catch ex As ApplicationExce ption
      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.Reflecti on.AmbiguousMat chException: 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.GetByt es(oaCopy)
      Line 18:
      Response.Output Stream.Write(en coding.GetBytes (oaCopy), 0, i)
      Line 19: Response.Flush( )

      Comment

      • Keith Patrick

        #4
        Re: Help streaming XML instead of dataset

        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.GetByt es(oa)

        with:
        encoding.GetByt es(oa.OuterXml)





        Comment

        Working...