I am looking for a method to stream XML formatted data from the database via an ASP.Net website. Our basic method for this has been:
- Create PL/SQL function which returns XMLType
- Convert the XMLType to a string via the "Value" property
- Set headers and write the string to the Response object
This typically works well enough as most of our downloads are fairly small so caching all the XML data on the server is not a big deal. With larger files though we need to load more and more data onto the server and so we started to investigate the possibility of streaming the data directly down to the user before we have it loaded fully using the OracleXMLStream object like so (omitting the connection open/close and specifics of the database query):
OracleXmlType xml = getXML();
OracleXmlStream xmlStream = new OracleXmlStream(xml);
Response.Clear();
Response.AddHeader("Content-Disposition","data.xml");
Response.ContentType = "text/xml";
byte[] buffer = new byte[0x1000];
int read;
while ((read = xmlStream.Read(buffer, 0, buffer.Length)) > 0)
Response.OutputStream.Write(buffer, 0, read);
Response.Flush();
Response.End();
This works, but the character encoding is preventing IE from reading the XML output. I can fix this by reading the character data out instead of the binary data and re-encoding myself using the character encoding classes, but is there another method I could use for this? Or am I going about this whole thing wrong?