Caching XML file when using XmlTextReader

Last post 07-02-2009 8:51 AM by katebp. 2 replies.

Sort Posts:

  • Caching XML file when using XmlTextReader

    07-01-2009, 11:07 AM
    • Member
      1 point Member
    • katebp
    • Member since 01-20-2009, 11:04 AM
    • London, UK
    • Posts 15

     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?
     

  • Re: Caching XML file when using XmlTextReader

    07-01-2009, 12:40 PM
    Answer

     I don't think caching an Xml(Text)Reader makes any sense as it is basically an API to parse through an XML document once in a forwards only manner. So what would you want to do with the XmlTextReader when you retrieve it from the cache? Reset it? That is not possible.

    You can try to cache the XPathDocument however, that is an in-memory data structure you should be able to reuse.

    Martin Honnen --- MVP XML
    My blog
  • Re: Caching XML file when using XmlTextReader

    07-02-2009, 8:51 AM
    • Member
      1 point Member
    • katebp
    • Member since 01-20-2009, 11:04 AM
    • London, UK
    • Posts 15

    Ah, brilliant - thanks.

    I am now trying to work out how to cache the xslt files, so I don't have to load each one...

    K

Page 1 of 1 (3 items)