C# - Mettere in cache dei file XML

Per evitare richieste al database, si possono mettere in cache alcune parti del sito. Questo script mostra un come si puņ sfruttare l'uso della Cache:

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data">
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Web.Caching" %>
<script runat="server"> 

void Page_Load(Object sender, EventArgs e) { / //Inserire il codice per popolare il datagrid nella pagina. //Segue un codice di esempio //-------------------------------------------------------- DataView myDataView = (DataView) GetDataViewFromCache(); DataTable myDataTable = myDataView.Table; DataGrid1.DataSource = myDataTable.DefaultView; DataGrid1.DataBind(); //-------------------------------------------------------- / } private DataView GetDataViewFromCache() { if ( Cache["FileInCache"'> == null ) InsertCache(); DataView dv = (DataView) Cache["FileInCache"'>; return dv; } private void InsertCache() { //Metto in cache il file Xml DataSet myDs = new DataSet(); FileStream fileXmlData; fileXmlData = new FileStream( Server.MapPath("xml/nomeFile.xml"), FileMode.Open, FileAccess.Read ); myDs.ReadXml(fileXmlData); Cache.Insert("FileInCache", new DataView(myDs.Tables[0'>), new CacheDependency( Server.MapPath("xml/nomeFile.xml") ) ); fileXmlData.Close(); } </script> <html> <head> </head> <body> <form runat="server"> <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid> </form> </body> </html>