This is driving me nuts. I have an XML file for a site:
<CWC_click_nav Created="1 August 2008">
<Level1>
<NavOption Label="Home">/</NavOption>
</Level1>
<Level1>
<NavOption Label="Conferences">/conference/</NavOption>
<Level2>
<NavOption Label="About CWC Conferences">/conference/about.aspx</NavOption>
<NavOption Label="Conference Team">/conference/team.aspx</NavOption>
</Level2>
</Level1>
<Level1>
<NavOption Label="Exhibitions">/exhibitions/</NavOption>
<Level2>
<NavOption Label="About CWC Exhibitions">/ex/about.aspx</NavOption>
<NavOption Label="Exhibition Team">/exhib/team.aspx</NavOption>
</Level2>
</Level1>
</CWC_click_nav>
In a Master file, I'm loading it up, checking the current directory and displaying the appropriate subnav. This worked a treat using this code (sorry about the code view, the post editor didn't want to display it properly):
void Page_Load()
{
if (!IsPostBack)
{
//Get the xml file
XmlTextReader xtr = new XmlTextReader(Server.MapPath("/CWC_click_nav.xml"));
xtr.WhitespaceHandling = WhitespaceHandling.None;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xtr);
//This is the list of top level nav options
XmlNodeList xNavList = xDoc.SelectNodes("CWC_click_nav/Level1");
//Start the nav list
litNav.Text += "<ul class=\"orange\">" + Environment.NewLine;
foreach (XmlNode xNavNode in xNavList)
{
//Process XML file, works a treat
}
//Finish the nav list
litNav.Text += "</ul>" + Environment.NewLine;
}
}
OK, so thats all good. I then decide to cache the XML file for perfomance reasons naturally. So my code changes to this:
void Page_Load()
{
if (!IsPostBack)
{
//HttpContext.Current.Cache.Remove("SiteNav");
if (HttpContext.Current.Cache.Get("SiteNav") == null)
{
litNavLoad.Text += "no cache.";
XmlTextReader xtrCache = new XmlTextReader(Server.MapPath("/CWC_click_nav.xml"));
HttpContext.Current.Cache.Insert("SiteNav", xtrCache, new CacheDependency(Server.MapPath("/CWC_click_nav.xml")));
litNavLoad.Text += "Data loaded from cache at " + DateTime.Now.ToString() + ".";
}
XmlTextReader xtr = (XmlTextReader)HttpContext.Current.Cache.Get("SiteNav");
xtr.WhitespaceHandling = WhitespaceHandling.None;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xtr);
//This is the list of top level nav options
XmlNodeList xNavList = xDoc.SelectNodes("CWC_click_nav/Level1");
//Start the nav list
litNav.Text += "<ul class=\"orange\">" + Environment.NewLine;
foreach (XmlNode xNavNode in xNavList)
{
//None of this happens!!!
}
//Finish the nav list
litNav.Text += "</ul>" + Environment.NewLine;
}
}
</script>
It works whenever I uncomment the
HttpContext.Current.Cache.Remove("SiteNav"); line, in which case it doesn't find the Cache object "SiteNav", recreates it, then loads the Cache object into the XmlTextReader. But if I then comment out that line and run the page again, I end up with an empty nav, just the opening and closing <ul> tags and no errors. I tried testing to see if the Cache was actually working with a string object, and thats fine, I could load it and read it again on multiple page visits. Its just this XML file which isn't working.
Any ideas?