0

ASP .Net app (Normal web page)

The below method is called on page load. For brevity i have substituted the For loop with an integer but in real terms this is a loop for getting Customers from a database:

Private Sub GenerateXml
    Dim XDoc As XDocument = <?xml version="1.0"?><Customers></Customers>
    Dim mystring As String = String.Empty

    For i As Integer = 0 To 10
        XDoc.Root.Add(<customer>
                          <id><%= i %></id>
                          <name>Test <%= i %></name>
                      </customer>)
    Next

    Using sw = New MemoryStream()
        Using strw = New StreamWriter(sw, System.Text.UTF8Encoding.UTF8)
            XDoc.Save(strw)
            mystring = System.Text.UTF8Encoding.UTF8.GetString(sw.ToArray())
        End Using
    End Using

    Response.Clear()
    Response.Buffer = True
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/xml"
    Response.Write(mystring)
    Response.Flush()
    Response.End()
End Sub

I started off getting the error "DTD must be defined before the document root element". The led to adding a MemoryStream and StreamWriter to avoid this issue. During testing this seemed to have worked. The next day i get a similar error but pointing to a line in Chrome (the error is intermittent):

This page contains the following errors: error on line 123 at column 8: Extra content at the end of the document Below is a rendering of the page up to the first error.

So I viewed the page from View Source went to line 123 and see this tag

</Customers><!DOCTYPE html>

(Note the error is at the end of the XML generated). Which has added the DOCTYPE in (No idea why)???? So searched on this error which led me to view the error logs on the server. The below error is listed on the server

Log Name:      Application
Source:        ASP.NET 4.0.30319.0
Event ID:      1309
Task Category: Web Event
Level:         Warning
Keywords:      Classic
User:          N/A

Description:
Event code: 3005 
Event message: An unhandled exception has occurred.  
Exception type: HttpException 

Exception message: Session state has created a session id, but cannot save it because the response was already flushed by the application.
   at System.Web.SessionState.SessionIDManager.SaveSessionID(HttpContext context, String id, Boolean& redirected, Boolean& cookieAdded)
   at System.Web.SessionState.SessionStateModule.CreateSessionId()
   at System.Web.SessionState.SessionStateModule.DelayedGetSessionId()
   at System.Web.SessionState.SessionStateModule.ReleaseStateGetSessionID()
   at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs)
   at System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

This makes me think the XML generated is fine but something else is causing an issue. The only thing i could think of was to remove the line

Response.Flush()

but I don't know if this would resolve the issue or not - I know i can try this but considering the error is intermittent i was looking for a complete solution in case someone knows what im doing wrong here?

Kᴀτᴢ
  • 2,146
  • 6
  • 29
  • 57
Computer
  • 2,149
  • 7
  • 34
  • 71
  • It *looks* like you just want to send data to the client, in which case an ashx handler would be easier to use. – Andrew Morton Mar 16 '16 at 10:07
  • All im trying to do is generate an XML so it can be viewed in a browser. When we go to the XML webpage to view it, thats when the error occurs. Refreshing the page resolves the issue, subsequent requests made are fine but after a while the error may pop up – Computer Mar 16 '16 at 10:17
  • I don't think that clearing the response will work. You need to create a new response from scratch. – jdweng Mar 16 '16 at 11:55
  • @jdweng Could you elaborate on that as the XML loads on Page Load (so everytime this page is accessed it loads the data)? – Computer Mar 16 '16 at 13:38
  • Use the same code that you use for first response. You should first make a request to the webpage. The response is the data returned from the request which is a new object for each request made not clearing the same response each time. – jdweng Mar 16 '16 at 15:47
  • @jdweng Sorry slightly confused here. Considering the code is on page load - how do i make a request to the same page? – Computer Mar 16 '16 at 16:04
  • If you use an ashx handler then you get to determine exactly what is returned; there are no headers pre-written for you or anything else to interfere with what you want to do. Here you go: [http://stackoverflow.com/a/2163400/1115360](http://stackoverflow.com/a/2163400/1115360). – Andrew Morton Mar 16 '16 at 18:11
  • Possible duplicate of [How to return XML in ASP.NET?](http://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net) – Andrew Morton Mar 16 '16 at 18:16
  • Move definition of variable(s) into global space on form instead of inside a method. Some variables can't be initialized in global space so the actual assignment is in the page load. Then later in other methods the same variable can be accessed. – jdweng Mar 16 '16 at 20:15

0 Answers0