Hello all,
I am currently working on a site when the navigation elements (top nav, left menu, breadcrumbs etc) are contained within user controls which individually load in the sitemap xml and use XslCompiledTransform to parse it through dfferent XSLT files.
I am currently working on how I can cache the sitemap xml so it isn't loaded from scratch four or five times every time the page is loaded.
from sitetemplate.master.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
using System.Xml.XPath;
public partial class sitetemplate : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
//validate all values in querystring
foreach (string i in Request.QueryString.AllKeys)
{
string ivalue = Request.QueryString[i];
if (!Regex.IsMatch(ivalue, "^[0-9a-zA-Z]"))
{
// invalid char. Redirect to pageid
Response.Redirect("error.aspx?error=invalchar");
}
}
// site xml
XmlTextReader siteXML;
// loading xml from cache
siteXML = (XmlTextReader)HttpContext.Current.Cache.Get("dotnettemplate");
Response.Write("// loading xml from cache ");
if (siteXML == null)
{
// not found in cache, retrieve from file
siteXML = new XmlTextReader(Server.MapPath("~/xml/dotnettemplate.sitemap"));
Response.Write("// not found in cache, retrieve from file");
// save to cache
Cache.Insert("dotnettemplate", siteXML, new CacheDependency(Server.MapPath("~/xml/dotnettemplate.sitemap")), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
}
//Cache.Remove("dotnettemplate");
XPathDocument xpathDoc = new XPathDocument(siteXML);
//Section menu
XmlTextReader sectionXSL = new XmlTextReader(Server.MapPath("~/xslt/SectionMenu.xslt"));
XslCompiledTransform SectionNavMenu = new XslCompiledTransform();
// Create the XsltArgumentList
XsltArgumentList xslArg = new XsltArgumentList();
SectionNavMenu.Load(sectionXSL);
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
SectionNavMenu.Transform(xpathDoc, xslArg, sw);
litSectionMenu.Text = sw.ToString();
}
sectionXSL.Close();
siteXML.Close();
xslArg.Clear();
sb = null;
}
}
The code appears to save the xml to the cache, but doesn't retrieve it properly.
If I substitute all the cache stuff above for:
XmlTextReader siteXML = new XmlTextReader(Server.MapPath("~/xml/dotnettemplate.sitemap"));
it works fine.
This post covered a similar problem: http://forums.asp.net/p/1300194/2539950.aspx#2539950 but I thought that XmlDocument shouldn't be used (and I couldn't get it to work with XmlTextReader any way).
Any ideas?