Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Last active August 29, 2015 17:34
Show Gist options
  • Save dieseltravis/23970 to your computer and use it in GitHub Desktop.
Save dieseltravis/23970 to your computer and use it in GitHub Desktop.
Caching an XML document with a file dependency
XmlDocument xDoc = null;
string xmlPath = "~/App_Data/SomeXml.xml";
if (HttpContext.Current.Cache[xmlPath] == null) {
string cacheFilePath = HttpContext.Current.Server.MapPath(xmlPath);
xDoc = new XmlDocument();
xDoc.Load(cacheFilePath);
HttpContext.Current.Cache.Insert(xmlPath, xDoc, new CacheDependency(cacheFilePath));
} else {
xDoc = (XmlDocument)HttpContext.Current.Cache[xmlPath];
}
Dim xDoc As XmlDocument
Dim xmlPath As String = "~/App_Data/SomeXml.xml"
If HttpContext.Current.Cache(xmlPath) Is Nothing Then
Dim cacheFilePath As String = HttpContext.Current.Server.MapPath(xmlPath)
xDoc = New XmlDocument()
xDoc.Load(cacheFilePath)
HttpContext.Current.Cache.Insert(xmlPath, xDoc, New CacheDependency(cacheFilePath))
Else
xDoc = DirectCast(HttpContext.Current.Cache(xmlPath), XmlDocument)
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment