Caching an XML file

Last post 08-06-2008 6:51 AM by kitster. 4 replies.

Sort Posts:

  • Caching an XML file

    08-04-2008, 8:49 AM
    • Member
      20 point Member
    • kitster
    • Member since 08-29-2007, 12:14 PM
    • London
    • Posts 31

    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? 

    Burn it, burn it all!
  • Re: Caching an XML file

    08-04-2008, 9:28 AM
    • Member
      20 point Member
    • kitster
    • Member since 08-29-2007, 12:14 PM
    • London
    • Posts 31

     UPDATE:

    I am now getting an error, which was being hidden by another .NET/IIS issue.

    On line

    xtr.WhitespaceHandling = WhitespaceHandling.None;

    I am now getting this error:

    Operation is not valid due to the current state of the object.

     

    [InvalidOperationException: Operation is not valid due to the current state of the object.]
    System.Xml.XmlTextReaderImpl.set_WhitespaceHandling(WhitespaceHandling value) +1959009
    System.Xml.XmlTextReader.set_WhitespaceHandling(WhitespaceHandling value) +11
    ASP.cwc_click_master.Page_Load() in d:\Shared\WEBSERVER\CWC_click\CWC_click.master:39
    System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o) +5
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +858603
    System.Web.UI.Control.OnLoad(EventArgs e) +99
    System.Web.UI.Control.LoadRecursive() +47
    System.Web.UI.Control.LoadRecursive() +131
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

     I presume this is because it hasn't been able to load the Cache object, so I put in a check to see if xtr was null:

                XmlTextReader xtr = (XmlTextReader)HttpContext.Current.Cache.Get("SiteNav");

                 if (xtr == null)
                    litNav.Text = "nothing";
                else
                    xtr.WhitespaceHandling = WhitespaceHandling.None;

    But its still erroring out.


    Burn it, burn it all!
  • Re: Caching an XML file

    08-05-2008, 7:33 AM
    • Member
      20 point Member
    • kitster
    • Member since 08-29-2007, 12:14 PM
    • London
    • Posts 31

     I am still stuck on this.  I have streamlined the code and found out exactly where the error is happening.  See below:

     

    1    XmlTextReader xtr;
    2 3 //HttpContext.Current.Cache.Remove("SiteNav"); 4 5 if (HttpContext.Current.Cache.Get("SiteNav") == null)
    6 {
    7 XmlTextReader xtrCache = new XmlTextReader(Server.MapPath("/CWC_click_nav.xml"));
    8 HttpContext.Current.Cache.Insert("SiteNav", xtrCache, new CacheDependency(Server.MapPath("/CWC_click_nav.xml")));
    9 litNavLoad.Text += "Data loaded from cache at " + DateTime.Now.ToString() + ".";
    10 xtr = (XmlTextReader)HttpContext.Current.Cache.Get("SiteNav");
    11 }
    12 else 13 {
    14 litNavLoad.Text += "Cached nav.";
    15 xtr = (XmlTextReader)HttpContext.Current.Cache.Get("SiteNav");
    16 }
    17 18 xtr.WhitespaceHandling = WhitespaceHandling.None;
    19

      The first time the page loads, HttpContext.Current.Cache.Get("SiteNav") is null, so it loads the xml file into cache, reads it back into the XmlTextReader and carries on processing past line 18.  However, the next time the page loads, HttpContext.Current.Cache.Get("SiteNav") is not null, line 15 loads the cache into the XmlTextReader exactly the same way as before (lines 10 and 15), but it error out immediately on line 18.

    I have tried loading the SiteNav cache object into an integer, just to see if it was being recognised as an XML file, and it gave me an incorrect type error, so it definitely thinks its an XML file in the cache.  Its just that after the first time the master page is loaded, the cache object exists but the content does not.

    Please, any ideas? 

    Burn it, burn it all!
  • Re: Caching an XML file

    08-06-2008, 6:17 AM
    Answer

    Hi kitster ,

    kitster:
    HttpContext.Current.Cache.Insert("SiteNav", xtrCache,

    Do not cache a XmlTextReader object , try to load it using XmlDocument then cache the XmlDocument object.

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Caching an XML file

    08-06-2008, 6:51 AM
    • Member
      20 point Member
    • kitster
    • Member since 08-29-2007, 12:14 PM
    • London
    • Posts 31

    That was it.  New code as follows:

     

    //Get the xml file
    XmlDocument xDoc = new XmlDocument();

    if (HttpContext.Current.Cache.Get("SiteNav") == null)
    {
    xDoc.Load(Server.MapPath("/CWC_click_nav.xml"));
    HttpContext.Current.Cache.Insert("SiteNav", xDoc, new CacheDependency(Server.MapPath("/CWC_click_nav.xml")));
    litNavLoad.Text += "Data loaded from cache at " + DateTime.Now.ToString() + ".";
    }
    else
    {
    litNavLoad.Text += "Cached nav.";
    xDoc = (XmlDocument)HttpContext.Current.Cache.Get("SiteNav");
    }
      This works a treat!  Many many thanks Samu Zhang.
    Burn it, burn it all!
Page 1 of 1 (5 items)