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?